我创建了一个带有静态数据成员的类。但是,即使.exe
没有创建文件,它也没有被执行。我正在使用Visual C++ express 2010
。
这是我的代码:
#include<iostream>
using namespace std;
class A
{
public:
static int a;
};
int main()
{
A::a = 10;
cout << A::a;
system("pause");
return 0;
}
编译时出现以下错误:
main.obj : error LNK2020: unresolved token (0A00038B) "public: static int A::a" (?a@A@@2HA)
1>main.obj : error LNK2001: unresolved external symbol "public: static int A::a" (?a@A@@2HA)
1>C:\Users\Labeeb\documents\visual studio 2010\Projects\static variables and functions\Debug\static variables and functions.exe : fatal error LNK1120: 2 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
只需将以下内容添加到您的源文件中:
int A::a;
static
成员变量需要在任何函数之外且在类声明之后的某个位置定义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句