最近在看单例设计模式从网上找了一个例子
class Singleton
{
private:
static Singleton* m_lpInstance;
Singleton(){};
public:
static Singleton *GetInstance()
{
if (NULL == m_lpInstance)
{
m_lpInstance = new Singleton();
}
return m_lpInstance;
}
};
main
{
Singleton *p1 = Singleton::GetInstance();
}
编译发现报错:
1>singletonModle.obj : error LNK2001: 无法解析的外部符号 "private: static class Singleton * Singleton::m_lpInstance" (?m_lpInstance@Singleton@@0PAV1@A)
1>E:\project\singletonModle\Debug\singletonModle.exe : fatal error LNK1120: 1 个无法解析的外部命令
http://blog.csdn.net/zhoxier/article/details/8619688
自己已经找到问题了所在了;
缺少了初始化:Singleton *Singleton ::m_lpInstance = NULL;
但是为什么会报无法解析的外部符号错误呢?
static Singleton* m_lpInstance;
因为对于静态变量来说,C++中规定了它需要初始化,如果不初始化就会找不到这个符号