我在 Stack.h 中编写了以下代码:
class Stack{
public:
inline bool full();
int size();
inline bool empty();
bool push(const string&);
bool pop(string &s);
bool peek(string &s);
virtual void print();
virtual ~Stack(){}
protected:
vector<string> _elem;
int const _maxsize=10; // line X
};
我得到了错误:
Stack.h:14: error: ISO C++ forbids initialization of member ‘_maxsize’
Stack.h:14: error: making ‘_maxsize’ static
make: *** [Stack.o] Error 1
如果我在 X 行添加一个 static 关键字,并在类定义之外初始化变量,它可能没问题。
但我的问题是,有没有可能的方法来声明一个非静态的 const 变量并仍然成功地初始化它???