Sign in to follow this  

Interesting linking issue with template functions....

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.h
struct A { int i; };
struct B { int i; };

template<typename T> T func(T in); // general declaration, no instantiation

void dumb();


//func.cpp
template<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.cpp
void 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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this