Static scope in Class

This is what i understood from reading a book, i made this example, it is not working. I understood that declaring a static scope variable in a class makes that variable independent of the class.

//program example

#include <iostream>
using namespace std;

class Point{
public:
static int x;
};

int main()
{
Point::x=0;
++Point::x;
++Point::x;
++Point::x;
cout<<Point::x;
}
closed account (zb0S216C)
You need to define it. Point::x was declared, but not defined. You can define it like so:

1
2
3
4
5
class Point
{
    //...
};
int Point::x(/*Some value*/); // Same as int Point::x = /*Some value*/; 

Wazzak
In your class you only declared static member 'x', but it was not defined. You shall define it the following way before 'main'

int Point::x;
thanks
defining the member did it
Topic archived. No new replies allowed.