• Advertisement
Sign in to follow this  

Template functions in classes separated from the class

This topic is 3390 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

Hi, I try to follow the source and header structure, where I defined the class in the header, and put the function code in the source file. However, I have a template function in my class, and want to put the code in the source file, however, it tells me the function is not defined (when I try to call it, it says undefined reference to). This is the (simplified) code:
class A
{
public:
    template <class T>
    void f(T t);
};

template <class T>
void A::f(T t);
{
    // function
}
I would expect it to work like this, but I get 'undefined reference to A::f(...)' whenever I call A::f() What is the correct notation? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Unless your compiler supports the export keyword, and 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.)

See also This thread currently in discussion.

Share this post


Link to post
Share on other sites
Hmm, it could be any class, so I suppose it should just stay in the header file.

Thank you! :)

Share this post


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

  • Advertisement