Sign in to follow this  

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

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

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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 3862 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.

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