我有一个类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]
我不明白为什么会这样; make_graph函数只返回一个Graph:
Graph<double,double> make_graph(string filename){...}
我需要一个'&amp;'某处?
答案 0 :(得分:5)
Read the answer here。换句话说,您错过了const
,而不是&
。成功:
template<typename Object,typename Weight>
Graph<Object,Weight>::Graph(const Graph<Object,Weight>& G)
您不能将临时绑定到非const引用。