30

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

frogatto
  • 28,539
  • 11
  • 83
  • 129
cppython
  • 1,209
  • 3
  • 20
  • 30
  • 4
    Inline initialization for members is a whole different ballgame. To take the direct equivalent of your first sample, you're supposed to initialize members in a _member initialization list_ (though there are limited cases where you can do it inline like you did in your second sample). – Lightness Races in Orbit Jan 16 '14 at 02:12
  • It seems to work with `int k[5] = {1,2,3,4,5};` but I can't explain why. – ValarDohaeris Jan 16 '14 at 02:18
  • @ValarDohaeris, It's more of a "it must have a size to do that in a class" thing. IIRC, the standard has an explicit rule. – chris Jan 16 '14 at 02:19

4 Answers4

38

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.

jtomschroeder
  • 1,034
  • 7
  • 11
3

In the first example, something (actual memory allocation) is actually happening. The computer easily counts the number of items given to it and uses this as the capacity. In the second use, as part of a struct, the array is simply part of the template of a struct. In the second case, a size must explicitly be given and known at compile-time. There is no automatic counting here. It's in a similar vein to function declarations versus definitions as well as variable declarations (tells type but memory is untouched) and their invocation/use (where the program acts).

Chris Zhang
  • 968
  • 7
  • 16
  • 1
    I'm finding it silly. Identical construct and it compiler can deduce size in one case but not in the other. How inconsistent is that? – There is nothing we can do Mar 15 '15 at 13:32
  • @Thereisnothingwecando The reason for this is that a `struct` can have a constructor, so that the array may be initialized in an alternative way, which could result in an incompatible type being deduced. This cannot happen for variables declared and initialized in global or function scope. It is related to [this](https://stackoverflow.com/questions/55026916/class-template-argument-deduction-in-member-variables) problem. – Ale Aug 05 '22 at 20:06
2

Probably cause in a struct the compiler needs you to specify the size explicitly.

C initialize array within structure (pun intended)

Community
  • 1
  • 1
Dannie
  • 2,430
  • 14
  • 16
1

These are two completely different contexts:

The first is a variable declaration (with an initialiser clause).

The second is a type definition.