我有一个单例类。
在啊
class single
{
public:
static single *Instance;
static single* getInstance()
{ if(!Instance) Instance = new single;
return Instance;
}
void hello () { cout<<"Hello"; }
private: single(){ }
}
在 A.cpp 中
single *single::Instance = 0;
std::auto_ptr <single> SINGLE_OBJ (single::getInstance());
在 B.cpp 中
#include "A.h"
SINGLE_OBJ->hello();
我收到以下错误: SINGLE_OBJ 未在此范围内声明。
最佳答案
要使 SINGLE_OBJ
在 B.cpp
中可见,您应该在 A.h.
中声明它,即:
extern std::auto_ptr <single> SINGLE_OBJ;
此外,您为什么要使用 std::auto_ptr
,它已被弃用 - 您应该切换到 std::unique_ptr
。
关于c++ - 多文件访问单例类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131944/