We have a singleton template class as defines below
template<class T> class Singleton{
T& reference(){
return objT;
}
private:
T objT;
};
And another user defined class which uses this singleton
class TestClass{
static Singleton<TestClass> instance;
static TestClass * getPointer()
{
return &instance.objT;
}
private:
TestClass(){}
};
template<TestClass>
Singleton<TestClass> TestClass::instance;
On compilation with GCC we are getting the error
In function static_initialization_and_destruction undefined reference to Singleton::Singleton().
What can be the reason for this.
Ignoring the fact, that in your example there is no need for Singleton template, consider this simplified example (I am using structs to avoid access issues):
To declare a member of type T, you need a complete definition of this type T. So, test1 and test2 are not legal – there is only a declaration, not definition of T. On the contrary, test3 is legal – it is located after the complete definition of the class. Easiest fix here is to use pointer to type – to declare a pointer to type T, you need a declaration instead of definition for the type T: