类的C 向量推回错误
C++ Vector of classes push back error
我是新来的C ,我使用G 编译器(Ubuntu 4.8.1-2ubuntu1〜12.04)4.8.1
我尝试实现无方向的图形。通过两个类边缘和图形。但是编译器给我这个错误
编译器给出此错误
/usr/include/c /4.8/ext/new_allocator.h:在实例化'void __gnu_cxx :: new_allocator< _tp> :: construct(_UP*,_ARGS&_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:需要'static typeName std :: enable_ifalloc> :: _ construct_helper< _tp, _args> :: value,void> :: type std :: allocator_traits< _alloc> :: _ s_construct(_Alloc&,_tp*,_args&_args&&&&& ...)[with _tp = undir_w_edge;_args = {const undir_w_edge&};_Alloc = STD ::分配器;打字 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,__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 ::分配器;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& amp; amp;) = undir_w_edge;_alloc = std ::分配器;std :: vector< _tp,_alloc> :: value_type = undir_w_edge]'' ../src/graph.h:31:5:'void graph :: addge(const edge&)[带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 :: forward< 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
的复制构造函数。
std::vector<UNDIR_W_EDGE>
内以及CompareTo
中需要复制构造函数,该构造符合其参数(出于某种原因)。
you 做有:
UNDIR_W_EDGE(UNDIR_W_EDGE &);
大概您打算是:
UNDIR_W_EDGE(const UNDIR_W_EDGE&);
- C++中函数的向量返回类型引发错误
- 尝试将unique_ptrs推送到向量时使用纯虚拟函数错误
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 使用全局声明的向量时,C++双重释放错误/损坏
- 代码使用向量成功运行,但使用数组显示错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 全局向量导致 C++ 程序结束时出现段错误
- 为什么我在向量初始化时收到此错误?
- 在C++中比较来自向量的字符串时出现分段错误
- 将图形表示为unordered_map<字符串、向量>时拓扑排序错误<string>
- 为什么在向量上使用 std::find() 时会出现错误?
- 分割错误:向量中的擦除功能
- 编译错误向量排序和联合
- SIGBART 错误向量<列表<myClass>>
- 编译错误 ::向量 ::迭代器
- 为什么 std::copy 抛出错误向量迭代器 + 偏移量超出范围并且无法复制
- C++STL set_union获取错误向量迭代器+偏移量超出范围
- C++错误向量无法正确输出