C ++类向量推回错误

莫阿门

我是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 && ...)[with _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&}; _Alloc = std :: allocator; 类型名称std :: enable_ifAlloc> :: _ construct_helper <_Tp,_Args> :: value,void> :: type = void]'/usr/include/c++/4.8/bits/alloc_traits.h:393:57:从'static必需注释类型(_S_construct(__ a,__p,(forward <_Args>)(std :: allocator_traits :: construct :: __ args)... ))std :: allocator_traits <_Alloc> :: construct(_Alloc&,_Tp *,_Args && ...)[with _Tp = UNDIR_W_EDGE; _Args = {const UNDIR_W_EDGE&}; _Alloc = std :: allocator; 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&)[需要_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&) '{::Args>( _ 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);
轨道轻度竞赛

您没有的副本构造函数UNDIR_W_EDGE

copy构造函数是内部必需的std::vector<UNDIR_W_EDGE>,并且内部CompareTo需要按值接受其参数(出于某种原因)。

这样做有这样的:

UNDIR_W_EDGE(UNDIR_W_EDGE &);

大概您打算使它成为:

UNDIR_W_EDGE(const UNDIR_W_EDGE&);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章