static member variable

I was just trying to do a simple test using a static member, but my code won't compile and I don't know what's wrong. I tried accessing the variable through and instance of the class as well, but that also didn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <conio.h>

using namespace std;

class A {
public:
	static int i;
};

int main() {
	A::i = 123;
	cout << A::i << endl;
	_getch();
	return 0;
}
Try with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>

using namespace std;

class A {
public:
	static int i;
};

int A::i; // <- add this!

int main() {
	A::i = 123;
	cout << A::i << endl;
	_getch();
	return 0;
}
Thanks!
Just to inform you, what that line means:

At this point, construct element 'i' in class 'A', with the default constructor.

You can call custom constructors from that line you just added.

You can also set its value to 123 on that line without doing it from main().
Topic archived. No new replies allowed.