[VC++ 2005] Template Class problem (kind of solved)

Started by
1 comment, last by IndyJones 16 years, 11 months ago
hey guys, i have a bit of a pickle and was wondering if anyone could shed some light on the problem or maybe my confusion since im kind of rusty, anyway, i have coded a template class and was going to test it out but i kept getting link errors and the linker kept saying it cant find the 'constructor' 'destructor' 'whatever member function being called' i didnt know why it kept saying that until i put the function bodies inside the class defintion it self which was in an include file, when the bodies are in the class it didnt have any problem linking, but if they are in a different .cpp file it would throw the linking errors. so all im asking is, do template classes have to be only in one file or is there a way around it, thanks. example of what im doing template header file:

template <class T>
class S
{
 public:
s();
~s();
}
template cpp file:

template<T> s::s()
{
..
..
..
}
test cpp file:

s<int> *object;

object = new s<int>();
object->func();
delete object;
Advertisement
The only way is to use the export keyword on your template, which VC++ 2005 does not support. So, you must include the definition of the template in at least one place where it is instantiated (the linker will then reuse that definition everywhere else).

It's good practice, however, to include the definition of the template in all places were it is instantiated.
You have to put the implementation of your template class into the header file (except your compiler supports the export keyword which afaik only the Comeau C++ compiler currently does).

This topic is closed to new replies.

Advertisement