Interesting linking issue with template functions....

Started by
4 comments, last by c_olin 16 years, 2 months ago
I have a class which contains template functions. For some reason the linker tells me that the functions are unresolved whenever I try to use one of the template functions. The interesting part is that if I call the template functions within the constructer of the class it is a method of, then it will link just fine. I've used the exact same code in another project so I don't think it is related to my code. If anyone is willing to help out but would like to see the code/linker errors let me know. Same thing happens for MingW32 and GCC (both running under Linux). Thanks
Advertisement
Quote:Original post by c_olin
If anyone is willing to help out but would like to see the code/linker errors let me know.

I would say it would be a good idea to show the code and the errors so that people can eliminate the possibility that it is the problem.

Are you getting unresolved external errors? If so, do you have the template functions in a seperate cpp file? Try putting them in the header.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
Template functions need to be inline, you can't export them from .cpp files (Until the export keyword comes in, but no compiler I know of actually supports it).
Quote:Original post by Evil Steve
Template functions need to be inline, you can't export them from .cpp files

No. Inline implementations must be known at compile time, while templates just require to be compiled; put the functions in the headers avoids linking problems in both cases, but they are not related.

The compiler must know in advance (before the linking) which templates have to be compiled. So you can define private templates (this "private" means a function called only in the file with its definition, it has nothing to do with a private member function) in the cpp without problems.
For public templates (called in other files, including the header with its declaration), you actually can define them in the cpp when you instatiate (using an implicit or explicit declaration) the template for all the types you need, e.g.
//func.hstruct A { int i; };struct B { int i; };template<typename T> T func(T in); // general declaration, no instantiationvoid dumb();//func.cpptemplate<typename T> T func(T in) // implementation{   return in; } void dumb() {   A a;   func(a); // implicit declaration, instantiate func<A>}template int func<int>(int in); // explicit declaration, instantiate func<int>//main.cppvoid main(){  A a;  func(a); // call instantiated func<A>  func(4); // call instantiated func<int>  B b;  //func(b) // this would not link, func<B> not instantiated}

If you move the main() to func.h nothing changes. If you move main() to func.cpp, the last line of main() would link.

Hope this helps!

Quote:Original post by c_olin
if I call the template functions within the constructer of the class it is a method of, then it will link just fine.

Implicit declaration.
I just inlined them and it linked fine.

Nyarlath

Yeah that all makes perfect sense. The only thing that is left unexplained is that I never had to explicitly define the functions in my old code. Only when I added it to my new project.

[Edited by - c_olin on February 8, 2008 3:41:08 PM]

This topic is closed to new replies.

Advertisement