[英]Initialize array in constructor's initialization list
I'm trying to initialize an array in my constructor's intialization list, and I want the array to have the size MAX_SIZE, which is a public static const in my Stack class. 我正在尝试在构造函数的初始化列表中初始化数组,并且希望该数组的大小为MAX_SIZE,这是Stack类中的公共静态const。 How can I get it to work?
我如何使它工作? The compiler complains, saying they have incompatible types in assignment of 'double' to 'double[0u]'
编译器抱怨说,在将“ double”分配给“ double [0u]”时,它们具有不兼容的类型
Here is my code: 这是我的代码:
class Stack {
public:
Stack();
static const unsigned MAX_SIZE;
private:
double array[];
unsigned elements;
}; // class Stack
Stack::Stack(): array( array[MAX_SIZE] ), elements(0) {}
const unsigned Stack::MAX_SIZE = 4;
Thanks in advance for your help. 在此先感谢您的帮助。
class Stack {
public:
Stack();
static const unsigned MAX_SIZE = 4;
private:
double array[MAX_SIZE];
unsigned elements;
}; // class Stack
Stack::Stack(): array(), elements(0) {}
But, std::vector
would be better as mentioned in the comments. 但是,如评论中所述,
std::vector
会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.