You can fix your error by adding a definition for the m_instance
member after the class definition.
template<typename T>
T* Singleton<T>::m_instance = nullptr;
For class templates, it's OK to add the definition of static
members within the header itself, and won't lead to ODR violations.
But, as others have suggested, it's best to change you getInstance()
definition to
static T& getInstance()
{
static T instance;
return instance;
}
C++11 even guarantees that the creation of the function local static
variable instance
will be thread-safe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…