Possible Duplicate:
Why should the implementation and the declaration of a template class be in the same header file?
I'm currently trying to implement a singleton template. I've taken the code straight from the german Wikipedia (you should be able to read the code). But I always get a weird compile error in Visual C++:
game.obj : error LNK2019: unresolved external symbol ""protected: __thiscall singleton<class game>::singleton<class game>(void)" (??0?$singleton@Vgame@@@@IAE@XZ)" in function ""protected: __thiscall game::game(void)" (??0game@@IAE@XZ)".
fatal error LNK1120: 1 unresolved externals.
(Running Visual Studio 2010)
I don't know what I did wrong in my code besides splitting it on multiple pages.
I define a template class singleton
that will be inherited by the class game
that should become the singleton.
singleton.hpp:
template <class T_DERIVED>
class singleton {
public:
static T_DERIVED& get_instance();
protected:
singleton();
private:
singleton(const singleton&);
singleton& operator=(const singleton&);
};
singleton.cpp:
#include "singleton.hpp"
template <class T_DERIVED>
singleton<T_DERIVED>::singleton()
{
}
template <class T_DERIVED>
T_DERIVED& singleton<T_DERIVED>::get_instance()
{
static T_DERIVED instance;
return instance;
}
template <class T_DERIVED>
singleton<T_DERIVED>& singleton<T_DERIVED>::operator=(
const singleton<T_DERIVED>&)
{
return *this;
}
game.hpp:
#include "singleton.hpp"
class game: public singleton<game> {
friend class singleton<game>;
protected:
game();
};
game.cpp:
#include "game.hpp"
game::game()
{
}
main.cpp:
#include "game.hpp"
#include <iostream>
int main()
{
game& a = game::get_instance();
return 0;
}