With this code:
http://www.rpi.edu/~doriad/Daniweb/maxflow/
If I run

g++ Example.cpp graph.cpp

with g++ 3.3, everything works fine. However if I run the same command with g++ 4.4, I get this error:

Example.cpp:(.text+0x38): undefined reference to `Graph<int, int, int>::Graph(int, int, void (*)(char*))'

Does anyone know how I can get this to work with g++ 4.4?

Thanks!

David

It compiles ok with VC++2008 Express too. I don't have g++ 4.4 so I can't test it.

I had to instantiate the constructor by adding this to the instances.inc file:

template Graph<int,int,int>::Graph(int, int, void (*)(char *));

Does anyone understand why / know if the compiler can be told to do this automatically? I thought the whole point of templates was so you did NOT have to know the types that would be used?

Thanks,

Dave

The reason (for good/bad) is that when the compiler hits instances.inc.
It has not seen the definition of graph.
If you put include instances at the end of graph.cpp all is fine.

Note that you only need the template instance for the class e.g. template class Graph<int,int,int>; since all the methods for the class are build. This should have been flaged as an error by VC++.
[in the same way that if you provide an explicit template function you should not explicitly instantiate it. ]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.