class Stack
{
private:
int tos;
const int max = 10;
int a[max];
public:
void push(int adddata);
void pop();
void printlist();
};
错误:无效使用非静态数据成员'max'
代码有什么问题,请帮我更正。 谢谢你
最佳答案
对于非堆分配(不使用 new
分配内存),必须在编译时知道数组大小。
如果您使用的是 C++11,constexpr
是一个很好的关键字,它是专门为此目的而设计的。 [编辑:正如@bornfree 在评论中指出的那样,它仍然需要是静态的]
static constexpr int max = 10;
因此,正如其他人指出的那样,使用 static
使其成为编译时间常量。
关于c++ - 错误 : invalid use of non-static data member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734072/