c++no-match

No matching function for call to copy constructor


I have a class Graph whose copy constructor is declared in Graph.h like this:

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

Elsewhere, I try to use it:

Graph<double,double> G = make_graph("dense.g");

...but it gives me the following error:

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){...} 

Do I need an '&' somewhere?


Solution

  • Read the answer here. In other words, you're missing a const, not an &. Make it:

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

    You can't bind a temporary to a non-const reference.