# Template functions in classes separated from the class

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

## 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 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.)

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

Thank you! :)

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 9
• 23
• 20
• 14
• 14
• ### Forum Statistics

• Total Topics
633383
• Total Posts
3011587
• ### Who's Online (See full list)

There are no registered users currently online

×