Rol*_*oós 2 c++ singleton boost
我尝试创建一个多线程单例模式类.
标题:
class HL{
public:
static HL* getInstance();
.........
private:
static HL* instance;
static boost::once_flag flag;
HL();
static void initOnce();
}
Run Code Online (Sandbox Code Playgroud)
CPP:
HL* HL::instance = NULL;
HL* HL::getInstance(){
if(instance == NULL){
boost::call_once(flag, initOnce);
}
return instance;
}
void HL::initOnce(){
instance = new HL();
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
error LNK2001: unresolved external symbol "private: static long Nsp::HL::flag" (?flag@HL@Nsp@@0JA)
怎么了?
您需要在cpp文件中定义静态成员变量:
boost::once_flag Nsp::HL::flag;
Run Code Online (Sandbox Code Playgroud)
如果需要,您可以初始化它(我没有使用过boost::once_flag,也无法告诉您是否需要初始化它):
boost::once_flag Nsp::HL::flag = {whatever goes here};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2064 次 |
| 最近记录: |