I was coding a class template to implement a Singleton when an issue occured.
While having a static pointer in a .h file, it wouldnt compile because of a linker issue (lnk 2001 and lnk 1120 on vs 15).
I Simplified my code to have the more understandable issue :
#pragma once #include "SingletonTemplate.h" int main() { SingletonTemplate<int>::test(); } my class SingletonTemplate.h (there is no .cpp btw)
template<class T>class SingletonTemplate { public: static int myInt; static void test() { SingletonTemplate<T>::myInt = 1; } } I read on several posts that the further declaration of the static variable can solve this issue. I added this outside of my function (but still in the .h) :
template< typename T > int SingletonTemplate<T>::myInt; Doing that solved my issue but I have no clue why. So, if someone could explain me what is the purpose of this line, it would be awesome.
Thanks in advance
SingletonTemplate<double>::myIntis a differentintfromSingletonTemplate<int>::myInt.