Jump to content
  • Advertisement
Sign in to follow this  
brwarner

Templated Function in Classes (In Libs)

This topic is 3778 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 regular class with a templated function inside it. This class is inside a static library and I want to use this class outside my library. Everytime I try to use it I get a LNK2019 that the function is not implemented. I'm assuming this is because the function was never generated inside the library because it was never used and, I wanted to know if there is any way to get this to work without making the library limit the template parameters by precreated some of them I need.

Share this post


Link to post
Share on other sites
Advertisement
Unless your compiler supports the export keyword, and seeing as that looks like a MSVC error code, I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

Share this post


Link to post
Share on other sites
you could try to use explicit instantiation
i know it works for class templates and function templates, but i'm not sure about a templated member function of a regular class

maybe this could work... try it
template void MyClass::myMethod<SomeSpecificType>(const SomeSpecificType& arg);

Share this post


Link to post
Share on other sites
I think I'll put it in the header to keep it flexible. But how does the STL library do it, they have tons of templated classes, do they all stay in the headers?

Share this post


Link to post
Share on other sites
Quote:
But how does the STL library do it, they have tons of templated classes, do they all stay in the headers?

Yep, all defined in headers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!