2012-04-29 65 views
4

我有一个类图,其拷贝构造函数声明中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<double,double> make_graph(string filename){...} 

我需要一个“&”的地方?

+0

可能重复的[C++模板:隐式转换,没有匹配函数调用ctor](http://stackoverflow.com/questions/2628646/c-templates-implicit-conversion-no-matching-function-for-打电话到构造函数) – outis 2012-04-29 04:15:12

回答

5

Read the answer here。换句话说,你错过了const,而不是&。创建它:

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

您不能将临时绑定到非const引用。