很简单的基础性东西求教
想学习一下单例模式,写了几行代码,但是编译不过,请大家指教,代码如下:
#include "stdafx.h"
#include<iostream>
#include <Windows.h>
using namespace std;
//测试单例模式
class A
{
private:
static A* mInstance;//A类的实例
public:
static A * GetInstance(){//各线程通过该方法取得同一个实例
if (NULL == mInstance)
{
mInstance = new A();
}
return mInstance;
}
void FuncA(char *);//各线程准备访问的方法,还没有写方法体
void FuncB(char *);//各线程准备访问的方法,还没有写方法体
};
A *mInstance = NULL;
DWORD WINAPI threadAFunc(LPVOID abc){//A线程函数
return 0;
}
DWORD WINAPI threadBFunc(LPVOID abc){//B线程函数
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
A* mA = A::GetInstance();//这句话编译出错
CreateThread(NULL, NULL, threadAFunc, NULL, NULL, NULL);
CreateThread(NULL, NULL, threadBFunc, NULL, NULL, NULL);
getchar();
return 0;
}
请帮忙看一下上面几行代码有什么问题吗? 现在在编译到A* mA = A::GetInstance();的时候,就报错:
error LNK2001: 无法解析的外部符号 "private: static class A * A::mInstance" (?mInstance@A@@0PAV1@A)
[解决办法]
A* A::mInstance = NULL;
[解决办法]
the definition of minstance should be written this way.
A* A::mInstance = NULL;