C++中静态成员变量如何访问?
迷茫
迷茫 2017-04-17 12:59:47
0
3
615

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;
}

这段代码中,会出现链接错误,请问是哪里出错了?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(3)
巴扎黑

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.

左手右手慢动作

Can you please put the statement at the front?

PHPzhong
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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template