【问题标题】:C++ Vector of classes push back error类的 C++ 向量推回错误
【发布时间】:2014-02-16 14:01:06
【问题描述】:

我是 C++ 新手,我使用 G++ 编译器 (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
我尝试实现无向加权图。由 Edges 和 Graph 两类。但是编译器给了我这个错误

编译器给出这个错误

/usr/include/c++/4.8/ext/new_allocator.h: 在 'void __gnu_cxx::new_allocator<_tp>::construct(_Up*, _Args&& ...) 的实例化中 [与 _Up = UNDIR_W_EDGE; _Args = {const UNDIR_W_EDGE&}; _Tp = UNDIR_W_EDGE]': /usr/include/c++/4.8/bits/alloc_traits.h:254:4: 来自'静态类型名 std::enable_ifAlloc>::_construct_helper<_tp _args>::value, void>::type std::allocator_traits<_alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = UNDIR_W_EDGE; _Args = {const UNDIR_W_EDGE&}; _分配= 标准::分配器;类型名称 std::enable_ifAlloc>::_construct_helper<_tp _args>::value, void>::type = void]' /usr/include/c++/4.8/bits/alloc_traits.h:393:57: 'static decltype (_S_construct(__a, __p, (forward<_args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [与_Tp = UNDIR_W_EDGE; _Args = {const UNDIR_W_EDGE&}; _分配= 标准::分配器; decltype (_S_construct(__a, __p, (forward<_args>)(std::allocator_traits::construct::__args)...)) = ]' /usr/include/c++/4.8/bits/stl_vector.h:906:34: 需要来自 'void std::vector<_tp _alloc>::push_back(const value_type&) [with _Tp = UNDIR_W_EDGE; _Alloc = std::allocator; std::vector<_tp _alloc>::value_type = UNDIR_W_EDGE]' ../src/GRAPH.h:31:5: 需要来自'void GRAPH::addEdge(const Edge&) [with Edge = UNDIR_W_EDGE]' ../src/main.cpp:32:13:从这里需要 /usr/include/c++/4.8/ext/new_allocator.h:120:4: 错误:没有匹配函数调用'UNDIR_W_EDGE::UNDIR_W_EDGE(const UNDIR_W_EDGE&)' { ::new((void *)__p) _Up(std::forwardArgs>(_args)...); }

代码

GRAPH.h 文件

#include "vector"

template <typename Edge>
class GRAPH {
 private:
 // Implementation-dependent code
int Vcnt;
int Ecnt;
std::vector<std::vector< Edge > > adj;
 public:
GRAPH(int x):Vcnt(x),Ecnt(0) {

    adj.resize(Vcnt);
}
 virtual ~GRAPH();
 virtual int V() const;
 virtual int E() const;
 virtual void addEdge(const Edge &e){
    adj[e.V()].push_back(e);
    adj[e.W()].push_back(e);
    Ecnt++;
 };
 virtual std::vector< Edge > adjIterator(int) const;
};

UNDIRWEDGE.h 文件

 class UNDIR_W_EDGE {
    int v,w;
    float weight;

    public:
     UNDIR_W_EDGE(int v, int w, float weight);
     UNDIR_W_EDGE(UNDIR_W_EDGE &);
     virtual ~UNDIR_W_EDGE();
     virtual int V()const;
     virtual int W()const;
     virtual float WEIGHT()const;
     virtual int CompareTo(UNDIR_W_EDGE e)const;
    };

在 UNDIRWEDGE.cpp 中

inline int UNDIR_W_EDGE::CompareTo(UNDIR_W_EDGE e)const{
if(this->weight > e.weight) return 1;
else if (this->weight < e.weight)return -1;
else                            return 0;
}  

在 main.cpp 中

GRAPH<UNDIR_W_EDGE> g(10);
UNDIR_W_EDGE e(0,7,0.0);
g.addEdge(e);

【问题讨论】:

  • 欢迎您!向我们展示您调试时使用的testcase,并格式化您的帖子以使错误可读,理想地点击提交之前。

标签: c++ templates c++11 vector stl


【解决方案1】:

您没有UNDIR_W_EDGE 的复制构造函数。

std::vector&lt;UNDIR_W_EDGE&gt;CompareTo 中需要复制构造函数,它按值(出于某种原因)获取其参数。

确实有这个:

UNDIR_W_EDGE(UNDIR_W_EDGE &);

大概你打算这样:

UNDIR_W_EDGE(const UNDIR_W_EDGE&);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 2021-02-19
    相关资源
    最近更新 更多