Thread: PLEASE HELP! initialization of static constant instance variables

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    PLEASE HELP! initialization of static constant instance variables

    I am reading a book and it says:

    Because the enum hack is so inelegant, the ANSI/ISO C++ working group has recently added a new construction to the language: initialization of static constant instance variables. This solves all the problems discussed in the sections above. Here’s the idea:

    class Stack
    {
    private:
    static const int size = 20; // static constant
    int st[size];
    int top;

    };

    However, when I tried to put this code in practise:

    #include <iostream.h>
    class Stack // a stack holds up to 20 ints
    {
    private:
    static const int size = 20; // array size
    int st[size]; // integers are stored in array
    int top; // index of last item pushed
    public:
    Stack() : top(-1) // constructor
    { }
    void push(int var) // place an item on the stack
    {
    st[++top] = var;
    }
    int pop() // remove an item from the stack
    {
    return st[top--];
    }
    };
    void main()
    {
    Stack s1; // create a stack object
    s1.push(11); // push 3 items onto stack
    s1.push(12);
    s1.push(13);
    cout << s1.pop() << endl; // pop 3 items and display them
    cout << s1.pop() << endl;
    cout << s1.pop() << endl;
    }


    Visual C++ 6 returns these errors:

    ompiling...
    prog.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(5) : error C2258: illegal pure syntax, must be '= 0'
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(5) : error C2252: 'size' : pure specifier can only be specified for functions
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(6) : error C2065: 'size' : undeclared identifier
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(6) : error C2057: expected constant expression
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(6) : warning C4200: nonstandard extension used : zero-sized array in struct/union
    C:\Program Files\Microsoft Visual Studio\MyProjects\arrayconst\prog.cpp(7) : error C2229: class 'Stack' has an illegal zero-sized array
    Error executing cl.exe.

    prog.obj - 5 error(s), 1 warning(s)




    Anybody know what the problem is?
    It's supposed to work isn't it?

    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Unregistered
    Guest
    > static const int size = 20; // static constant
    This has to be declared and initialized outside of the class, something like this
    Code:
    class Stack
    {
    	private:
    		int st[size]; //*WRONG!*
    		int top;
    	...
    };
    
    static const int Stack::size = 20; // static constant
    See the problem yet? Using size in this way probably won't work, but you can try it and see. Since you're declaring and initializing the static const properly then when you instantiate the object it could very well accept taking the constant as the size of the array...but I doubt it.

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Anybody know what the problem is?
    You can't do it in VC++ 6.0, so you'll have to keep using the enum hack. It's supported in VC++.NET, though.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM