가능한 중복 :
What is an undefined reference/unresolved external symbol error and how do I fix it?정적 멤버 변수
왜 나는 다음과 같은 코드에 대한 오류는 "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;
}
은 정적 변수가 아니며 기본적으로 0으로 초기화됩니까? – user673769
@ user673769 : 예, §8.5/6은 모든 정적 객체가 최소 0으로 초기화되도록 보장합니다. 따라서 정의를'int Monitor :: count;'로 줄이면된다.하지만 정의가 필요하다. – ildjarn