#include <iostream>
using namespace std;
class A {
private:
A(){};
static A *a;
public:
static A &getA(){
if(a==NULL){
a = new A();
}
return a;
}
A::getA();
system("pause");
return 0;
}
编译提示下面错误:
1>test141130_sin.obj : error LNK2001: 无法解析的外部符号 "private: static class A * A::a" (?a@A@@0PAV1@A)
using namespace std;
class A {
private:
A(){};
static A *a;
public:
static A &getA(){
if(a==NULL){
a = new A();
}
return a;
}
};
A::getA();
system("pause");
return 0;
}
编译提示下面错误:
1>test141130_sin.obj : error LNK2001: 无法解析的外部符号 "private: static class A * A::a" (?a@A@@0PAV1@A)
原因:
私有静态成员变量在使用前必须初始化,在.cpp文件中加入:A*
A::a=null;