use*_*665 1 c++ linker singleton static linker-errors
自从我用C++编程以来已经有一段时间了.我试图实现一个单例类,但我得到一个未解析的外部符号.你们能指出解决这个问题吗?提前致谢!
class Singleton
{
Singleton(){}
Singleton(const Singleton & o){}
static Singleton * theInstance;
public:
static Singleton getInstance()
{
if(!theInstance)
Singleton::theInstance = new Singleton();
return * theInstance;
}
};
Run Code Online (Sandbox Code Playgroud)
错误:
错误3错误LNK1120:1个未解析的外部
错误2错误LNK2001:未解析的外部符号
"private: static class Singleton * Singleton::theInstance" (?theInstance@Singleton@@0PAV1@A)
您已声明 Singleton::theInstance,但尚未定义它.在一些.cpp文件中添加其定义:
Singleton* Singleton::theInstance;
Run Code Online (Sandbox Code Playgroud)
(另外,Singleton::getInstance应该返回Singleton&而不是Singleton.)
| 归档时间: |
|
| 查看次数: |
5921 次 |
| 最近记录: |