First:
int k[] ={1,2,3,4,5};
Second:
struct slk { int k[] ={1,2,3,4,5}; };
for those two statements, why does the first one pass the compilation but the second one give me
error:too many initializers for 'int [0]'. the compilation would passed if I set k[5];
What does this error message means? Note: code tested on GNU GCC version 4.7.2
In C++11, in-class member initializers are allowed, but basically act the same as initializing in a member initialization list. Therefore, the size of the array must be explicitly stated.
Stroustrup has a short explanation on his website here.
The error message means that you are providing too many items for an array of length 0, which is what int []
evaluates to in that context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With