Sign in to follow this  
Decrius

Template functions in classes separated from the class

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
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

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