Jump to content
  • Advertisement
Sign in to follow this  
Tera_Dragon

unresolved external symbol

This topic is 4701 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 have implemented a resource manager similar to the one show here, though I am getting "unresolved external symbol" errors. I have a model manager derived from the TPersistentManager, though any of the TPersistentManager methods called by the model manager give the error, even the constructor and destructor. The code is as follows:
/* TPersistentManager.h */
template <class T> class TPersistentManager
{
...
};

/* TPersistentManager.cpp */
#include "TPersistentManager.h"

template <class T>
TPersistentManager<T>::TPersistentManager()
{
...
}

/* ModelManager.h */
#include "TPersistentManager.h"

class ModelManager : public TPersistentManager<Resources::Model>
{
...
};

/* ModelManager.cpp */
ModelManager::ModelManager()
{
}
The model manager constructor gives the error: error LNK2019: unresolved external symbol "public: __thiscall TPersistentManager<class Resources::Model>::TPersistentManager<class Resources::Model>(void)" (??0?$TPersistentManager@VModel@Resources@@@@QAE@XZ) referenced in function "public: __thiscall ModelManager::ModelManager(void)" (??0ModelManager@@QAE@XZ) I'm not very familiar with templates, so is this something to do with the way I am using them?

Share this post


Link to post
Share on other sites
Advertisement
You can't separate the implementation of a template from its declaration that way. Code that is going to instanciate a template needs access to the whole template's code.

The C++ compilation model is such that each .cpp file is compiled independently from the others. While processing TPersistentManager.cpp, the compiler is completely ignorant of ModelManager.cpp, and vice versa.

Class templates do not, in and of themselves, cause the generation of any code. How could they, when they are incomplete? Only instanciations of the template, which produces "real" classes do so. If you were to write: class TPersistentManager<int>;, that declaration would trigger the instanciation of the class template with an int parameter, and generate the appropriate code -- assuming the implementation is available.

Now, when the compiler processes ModelManager.cpp, it tries to instanciate TPersistentManager<Resources::Model>, but the implementation, which you have stashed in TPersistentManager.cpp is unavailable to it. How is the compiler supposed to know where it is and what to do? It just can't.

This is why you should put the whole class template in the header file that will get included in ModelManager.cpp. If you don't, you will not be able to instanciate the template with arbitrary parameters.

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!