简体   繁体   中英

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. How can I get it to work? The compiler complains, saying they have incompatible types in assignment of 'double' to '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM