相关疑难解决方法(0)

类中的静态成员变量

可能重复:
什么是未定义的引用/未解析的外部符号错误,如何解决?

为什么我Monitor::count对以下代码有"未定义的引用"错误?谢谢!

#include <iostream>

using namespace std;

class Monitor
{
    static int count;
public:
    void print() { cout << "incident function has been called " << count << " times" << endl; }
    void incident() { cout << "function incident called" << endl; count++; }
};

void callMonitor()
{
    static Monitor fooMonitor;
    fooMonitor.incident();
    fooMonitor.print();
}

int main()
{
    for (int i = 0; i < 5; i++)
        callMonitor();
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

c++ static class

1
推荐指数
1
解决办法
4825
查看次数

标签 统计

c++ ×1

class ×1

static ×1