I'm trying to understand the meaning of the error I get.
This is a singleton implementation:
class Singleton
{
private:
Singleton():m_value(0){};
static Singleton * m_instance;
int m_value;
public:
static Singleton * GetInstance()
{
if(!m_instance)
{
m_instance = new Singleton;
}
return m_instance;
}
void SetValue(int x){m_value = x;}
int GetValue(){return m_value;}
~Singleton()
{
if(m_instance)
delete m_instance;
}
};
Singleton* Singleton::m_instance = 0;
void main()
{
Singleton * s1 = Singleton::GetInstance();
}
The code compiles and run successfully.
When I remove the line Singleton* Singleton::m_instance = 0;
, I get the error:
error LNK2001: unresolved external symbol "private: static class Singleton * Singleton::m_instance"
I guess the meaning of that line is to set the static variable m_instance
to 0.
So I don't understand the syntax of that line- why can't I write just Singleton::m_instance = 0;
? and also why do I get linkage error when removing that line?