Static int inside struct is undefined !!

Why static int is not defined inside struct abc..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;
struct abc
{
static int x;
};
//int abc::x;
int main()
{
abc obj1;
obj1.x = 10;
cout << "Value of 10 = " << obj1.x << endl; 
return  0;
}
This code produces error.
undefined reference to X.
But If I uncomment line above int main() then this code works.
Static members have to be defined outside the class somewhere, like you're doing on line 7.
Topic archived. No new replies allowed.