C++ Class Constant Defining

I'm writing a program for a class where I have to make a stack and do some stuff with it. I got it all done and it runs fine when I compile it, but I'm getting a warning about my MAX_STACK. I try initializing it in the header file like it is below and I get the warning, but if I don't initialize it and instead try to initialize it in one of the constructors I get an error.

Normally I wouldn't care about this, but I have to transfer the code to Linux and it won't transfer because of this error, "error: ISO C++ forbids initialization of member MAX_STACK" is what it says.

If you need to see my public functions or my main file I can put those in too but I feel like it should just be a straightforward fix that I'm not getting.

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
#ifndef CONGERA2_H
#define CONGERA2_H

typedef float Element300;

class Stack300
{
    public:
        Stack300 ();
        Stack300 (const int);
        Stack300 (Stack300 &old);
        ~Stack300();
        void push300(const Element300);
        Element300 pop300();
        void viewTB300();
        void viewBT300();

     private:
        const int MAX_STACK = 10;
        Element300 * stackArray;
        int top;

};

#endif 


 
you cannot initialize a const member in the class declaration unless it is static

initialize MAX_STACK in the initializer list.
Where is the initializer list located?
Initialize list is not for setting const value.
In this scenario, you'd better put it into a anonymous enum.

1
2
3
4
5
6
7
class Stack300
{
public:
    enum { MAX_STACK = 10 };  // define the constant.
    Stack300();
    ....
};

as long it is not static you can.
Topic archived. No new replies allowed.