How can I declare and use static variables within a class?

I am trying to get a quick example to work for a static variable within a class.
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//test
#include <iostream>

using namespace std;

class stack
{
    private:
        static int stack_count; // number of stacks

	public:
		stack()
		{
		    stack_count++;
		}

		~stack()
		{
		    stack_count--;
		}



		static int get_count()
		{
		    return stack_count;
		}

        int stack::stack_count = 0;



};

int main()
{
    stack a_stack;

    cout << "Stack_count = " << stack::get_count() << endl;

    return 0;
}


Here is the error produced from gcc
1
2
3
4
5
6
static_test.cpp:29: error: extra qualification ‘stack::’ on member ‘stack_count’
static_test.cpp:29: error: ISO C++ forbids initialization of member ‘stack_count’
static_test.cpp:29: error: making ‘stack_count’ static
static_test.cpp:29: error: ISO C++ forbids in-class initialization of non-const static member ‘stack_count’
static_test.cpp:29: error: declaration of ‘int stack::stack_count’
static_test.cpp:9: error: conflicts with previous declaration ‘int stack::stack_count’
Move line 29 below the class declaration.
Thank you. I was a little unsure about the placement of that initialization.
Topic archived. No new replies allowed.