Jump to content
  • Advertisement
Sign in to follow this  
hanckjt

Use template in VC's problem

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

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?

Share this post


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

Share this post


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

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

Share this post


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

template <class T>
class CTemplate
{
public:
CTemplate(void);
virtual ~CTemplate(void);
};
template <class T>
CTemplate<T>::CTemplate(void)
{
}

template <class T>
CTemplate<T>::~CTemplate(void)
{
}


Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!