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