Sign in to follow this  
Tera_Dragon

unresolved external symbol

Recommended Posts

Tera_Dragon    260
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
Fruny    1658
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

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