4

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?

mtvec
  • 17,846
  • 5
  • 52
  • 83
norman
  • 5,128
  • 13
  • 44
  • 75
  • possible duplicate of [C++ Templates: implicit conversion, no matching function for call to ctor](http://stackoverflow.com/questions/2628646/c-templates-implicit-conversion-no-matching-function-for-call-to-ctor) – outis Apr 29 '12 at 04:15

1 Answers1

5

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.

Community
  • 1
  • 1
Cornstalks
  • 37,137
  • 18
  • 79
  • 144