The code is as below:
#include <iostream>
using namespace std;
class A {
static int id_;
public:
static void setId(int id) {
id_ = id;
}
static int getId() {
return id_;
}
};
int main()
{
A::setId(10);
cout << A::getId() << endl;
return 0;
}
When I compile it in Xcode, Mac OS, there's an error message:
Undefined symbols for architecture x86_64:
"A::id_", referenced from:
A::setId(int) in main.o
A::getId() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I add the line:
int A::id_ = 10;
before the main()
. Then, everything is fine. What's the reason with it?