Use template in VC's problem

Started by
5 comments, last by Sr_Guapo 17 years, 11 months ago
I declare a class in "ct.h" file, and define it in "ct.cpp" file. The class is using template. ^^^^^^^^ Problem: I want to use this class in other cpp file of the same project. if (#include "ct.h") then VC tell me:"LNK2019:unresolved external symbol" if (#include "ct.cpp") then VC tell me:"That's all right!" And somebody tell me that's the VC's problem: In VC template class must declare and define in same file. That's why?
Advertisement
Some compilers require that the entire template code be included whenever it is to be used (i.e. both the interface and implementation).
The general way I've seen this done is that the template implementation is split into a *.tem file as opposed to a *.cpp file, and then is 'included' at the end of the *.h file. Then when another class wishes to use the template class, they only include the .h file (which in turn includes the implementation file). Calling the file *.tem instead of *.cpp is only significant in that it will allow makefiles to still compile using wildcards on *.cpp without 'accidently' trying to compile an empty template.
I know this is the case with GCC compiler.

Having said that, I was under the impression that VC++ allowed a template's interface and implementation to be separated like any other code????

Without seeing your code, it's hard to say whether it's the issue I described above or something in your own code structure.
OK,This is a simple test code in VC++.NET2003.

main.cpp:
#include "Template.h"int main(int argc, char* argv[]){	CTemplate<int> tmp;	return 0;}



Template.h:
#pragma oncetemplate <class T>class CTemplate{public:	CTemplate(void);	virtual ~CTemplate(void);};



Template.cpp:
#include "template.h"template <class T>CTemplate<T>::CTemplate(void){}template <class T>CTemplate<T>::~CTemplate(void){}


Then VC++ show:
Linking.......
TestTemplate.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CTemplate<int>::~CTemplate<int>(void)" (??1?$CTemplate@H@@UAE@XZ) referenced in function _main
TestTemplate.obj : error LNK2019: unresolved external symbol "public: __thiscall CTemplate<int>::CTemplate<int>(void)" (??0?$CTemplate@H@@QAE@XZ)referenced in function _main
Quote:Original post by hanckjt
OK,This is a simple test code in VC++.NET2003.

main.cpp:
*** Source Snippet Removed ***


Template.h:
*** Source Snippet Removed ***


Template.cpp:
*** Source Snippet Removed ***

Then VC++ show:
Linking.......
TestTemplate.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CTemplate<int>::~CTemplate<int>(void)" (??1?$CTemplate@H@@UAE@XZ) referenced in function _main
TestTemplate.obj : error LNK2019: unresolved external symbol "public: __thiscall CTemplate<int>::CTemplate<int>(void)" (??0?$CTemplate@H@@QAE@XZ)referenced in function _main


The problem isn't VC's. Template classes have to be defined in every compilation unit using them. So you will have to move the implementation to the header.

Template.h
#pragma oncetemplate <class T>class CTemplate{public:	CTemplate(void);	virtual ~CTemplate(void);};template <class T>CTemplate<T>::CTemplate(void){}template <class T>CTemplate<T>::~CTemplate(void){}

Quote:Original post by tuita
....
The general way I've seen this done is that the template implementation is split into a *.tem file as opposed to a *.cpp file, and then is 'included' at the end of the *.h file. Then when another class wishes to use the template class, they only include the .h file (which in turn includes the implementation file). Calling the file *.tem instead of *.cpp is only significant in that it will allow makefiles to still compile using wildcards on *.cpp without 'accidently' trying to compile an empty template.
....


I have tried this way,that's all right.
Thanks tuita for your help.
Quote:Original post by CTar

The problem isn't VC's. Template classes have to be defined in every compilation unit using them. So you will have to move the implementation to the header.



You mean this "problem" is a rule in C++, for all C++ compiler not only VC++?
Quote:Original post by hanckjt
Quote:Original post by CTar

The problem isn't VC's. Template classes have to be defined in every compilation unit using them. So you will have to move the implementation to the header.



You mean this "problem" is a rule in C++, for all C++ compiler not only VC++?


That is the correct way (as taught by my CS instructors). Certain compilers may allow separating them out, but I still wouldn't do it.
Sean Henley [C++ Tutor]Rensselaer Polytechnic Institute

This topic is closed to new replies.

Advertisement