class Tank
{
public:
static int getCount()
{
return count;
}
static int count;//定义一个静态成员变量
};
int main(void)
{
Tank::count = 0;
//cout << Tank::getCount() << endl;
cout << Tank::count << endl; //无法运行
system("pause");
return 0;
}
这段代码中,会出现链接错误,请问是哪里出错了?
巴扎黑2017-04-17 13:01:47
Did you write the initialization wrong? It should be
int Tank::count = 0;
int main(void)
{
cout << Tank::count << endl; //无法运行
system("pause");
return 0;
}
Place initialization outside main.
PHPz2017-04-17 13:01:47
class Tank
{
public:
static int getCount()
{
return count;
}
static int count;//声明一个静态成员变量
};
//定义并初始化count
int Tank::count = 0;
int main(void)
{
Tank::count = 0;
//cout << Tank::getCount() << endl;
cout << Tank::count << endl; //无法运行
system("pause");
return 0;
}