Initializing Array in Class Definition

So, this surprised me a little. I’d appreciate it if someone more skilled in the nuances of C++ (many of you, I’m sure) could explain why the initialization of the array in ‘class2’ causes the noted compiler error.

Thanks.

const uint8_t globalArray[] = {1, 2, 3};   // <--- Compiles fine (of course)

class class1 {
  private:
    const uint8_t class1Array[3] = {1, 2, 3};   // <--- Compiles fine
};

class class2 {
  private:
   const uint8_t class2Array[] = {1, 2, 3};
    /*
      Compiler Error:
     
      sketch_sep24c:10: error: too many initializers for 'uint8_t [0] {aka unsigned char [0]}'
      uint8_t class1Array[] = {1, 2, 3};

                                     ^
      too many initializers for 'uint8_t [0] {aka unsigned char [0]}'
    */
};


void setup() {}
void loop() {}

In the header file, you DEFINE the class. In the source file, you IMPLEMENT the class.

You can only declare an array, not initialize it, in the header file, unless the array is const (in which case you must initialize it).

You assign values to the array in the source file.

PaulS:
You can only declare an array, not initialize it, in the header file, unless the array is const (in which case you must initialize it).

Sorry Paul, right after I posted the question I updated the code to make the array constant. Same error. My question is really, why does array initialization work in 'class1' and not in 'class2'? Only difference is [] vs [3].

Your revised code says that class2Array is const, but the compiler seems to disagree with you. The array statement that it shows doesn't have the const keyword.

It also shows that the array length is 0, suggesting that it is ignoring the initializers that shouldn't be there (because the array isn't const).

So, did you actually compile the code after adding the const keyword?

That's what I get for trying to update things too quickly. Should have done it in a new reply. Here's code I've actually compiled along with the error:

const uint8_t globalArray[] = {1, 2, 3};   // <--- Compiles fine (of course)

class class1 {
  private:
    const uint8_t class1Array[3] = {1, 2, 3};   // <--- Compiles fine
};

class class2 {
  private:
    const uint8_t class2Array[] = {1, 2, 3};
    /*
      Arduino: 1.8.5 (Windows 10), TD: 1.40, Board: "Arduino/Genuino Uno"

      sketch_sep24c:10: error: too many initializers for 'const uint8_t [0] {aka const unsigned char [0]}'

      const uint8_t class2Array[] = {1, 2, 3};                                      ^

      exit status 1
      too many initializers for 'const uint8_t [0] {aka const unsigned char [0]}'
    */
};


void setup() {}
void loop() {}

And, just to put a finer point on it, this compiles without error:

const uint8_t globalArray[] = {1, 2, 3};   // <--- Compiles fine (of course)

class class1 {
  private:
    const uint8_t class1Array[3] = {1, 2, 3};   // <--- Compiles fine
};

class class2 {
  private:
    //const uint8_t class2Array[] = {1, 2, 3};
};


void setup() {}
void loop() {}

So, as I see it, the problem lies with the [] construct verses putting in the array size: [3].

The answer appears to be "because that's the way C++ works":