C++ 类 vector 推回错误

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

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

编译器给出这个错误

/usr/include/c++/4.8/ext/new_allocator.h: In instantiation of '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: required from 'static typename 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; typename std::enable_ifAlloc>::_construct_helper<_Tp, _Args>::value, void>::type = void]' /usr/include/c++/4.8/bits/alloc_traits.h:393:57: required from 'static decltype (_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: required from '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: required from 'void GRAPH::addEdge(const Edge&) [with Edge = UNDIR_W_EDGE]' ../src/main.cpp:32:13: required from here /usr/include/c++/4.8/ext/new_allocator.h:120:4: error: no matching function for call to '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它按值获取参数(出于某种原因)。

确实有这个:

UNDIR_W_EDGE(UNDIR_W_EDGE &);

大概您希望它是:

UNDIR_W_EDGE(const UNDIR_W_EDGE&);

关于C++ 类 vector 推回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351933/

相关文章:

c++ - 如何将非静态成员函数作为模板参数传递给另一个成员函数?

c++ - 唯一 ptr 初始化断言失败

c++ - 是否可以使用参数包而不是嵌套模板序列?

c++ - 如何在模板类中调用继承类的函数

c++ - 如何判断库是使用 C++11 编译的

c++ - 没有冲洗的自定义ostream?

c++ - 访问模板类对象 vector 中的对象

c++ - std::function 中的可变模板参数匹配

c++ - 这是优化器的怪癖还是语言规则禁止优化的结果?

c++ - 等待 COM 端口上的数据?