【发布时间】:2014-03-11 03:40:00
【问题描述】:
我创建了一个小程序,它使用静态 void 函数来获取数字和另一个静态 void 函数来显示数字。但是当应用程序运行时,它会给我这个错误
错误 1 错误 LNK2001: 无法解析的外部符号 "private: static int 事物::i" (?i@Thing@@0HA)
这是我的代码
#include <iostream>
using namespace std;
class Thing{
private:
static int i;
public:
static void set_i(int h){
i = h;
}
static void print_i(){
cout << "The value of i is: " << i << endl;
}
};
int main(){
Thing::set_i(25);
Thing::print_i();
system("pause");
return 0;
}
【问题讨论】:
-
你必须在类外初始化静态值 ..int Thing::i = 0;
标签: c++ class runtime-error static-members