我有一个类 Graph,其复制构造函数在 Graph.h 中声明如下:

template<typename Object,typename Weight>
Graph<Object,Weight>::Graph(Graph<Object,Weight>& G)

在其他地方,我尝试使用它:
Graph<double,double> G = make_graph("dense.g");

...但它给了我以下错误:
time_trialsALIST.cpp:37: error: no matching function for call to `Graph::Graph(Graph)'
Graph.h:142: note: candidates are: Graph::Graph(Graph&) [with Object = double, Weight = double]

I don't understand why this would happen; the make_graph function just returns a Graph:

Graph<double,double>  make_graph(string filename){...}

我需要在某处使用 '&' 吗?

最佳答案

Read the answer here 。换句话说,您缺少的是 const ,而不是 & 。做了:

template<typename Object,typename Weight>
Graph<Object,Weight>::Graph(const Graph<Object,Weight>& G)

您不能将临时对象绑定(bind)到非常量引用。

关于c++ - 没有用于调用复制构造函数的匹配函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10369544/

10-13 06:18