简体   繁体   English

在构造函数的初始化列表中初始化数组

[英]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.

相关问题 C ++在构造函数的初始化列表中初始化嵌套结构? - C++ Initialize nested struct in constructor's initialization list? 是否可以在构造函数的初始化列表中初始化新对象并对其进行引用? - Is it possible to initialize new object in constructor's initialization list and reference it? 在C ++中初始化构造函数初始化列表中的char数组 - Initialize array of char in initialization list of constructor in C++ 在类构造函数初始化列表中初始化对象数组的第一个条目 - Initialize first entries of an array of objects in class constructor initialization list 如何在构造函数的初始化列表中初始化共享指针? - How to initialize a shared pointer in the initialization list of a constructor? 如何通过构造函数通过成员初始化列表初始化C风格的char数组和int数组? - How to initialize C-style char array and int array via member initialization list through constructor? 当数组的大小是模板参数时,如何在构造函数初始化列表中初始化 std::array 成员 - How to initialize std::array member in constructor initialization list when the size of the array is a template parameter 如何为std :: array的列表初始化构造函数编写包装器? - How to write a wrapper for std::array's list initialization constructor? 在构造函数的初始化列表上初始化数组或向量 - Initialize array or vector over constructor's initializing list 零初始化初始化列表中的数组成员 - Zero-Initialize array member in initialization list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM