Possible Duplicate:
Defining static members in C++
Static method with a field
I found the below code on singleton implementation on the web, and decide to give it a try:
#include <iostream>
class Singleton
{
Singleton(){}
static Singleton *s_instance;
public:
static Singleton* getInstance()
{
if(!s_instance)
s_instance = new Singleton();
return s_instance;
}
};
int main()
{
Singleton::getInstance();
return(0);
}
It looks quite straight forward. But when I build it in Visual Studio, it gives a linker error message:
main.obj : error LNK2001: unresolved external symbol "private: static class Singleton
* Singleton::s_instance" (?s_instance@Singleton@@0PAV1@A)
C:\Users\boll\Documents\Visual Studio 2010\Projects\hello_world\Debug\hello_world.exe :
fatal error LNK1120: 1 unresolved externals'
Why is 's_instance
' unresolved in this case?