4

I have implemented following structures:

struct Point {
    int x,y;
};

struct Array {
    Point elem[3];
};

Could you explain why I'm getting an error:

error: too many initializers for 'Array'

when I use following construction?:

Array points2 {{1,2},{3,4},{5,6}};
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
Leopoldo
  • 795
  • 2
  • 8
  • 23

2 Answers2

10

You need more braces, since you're initialising objects within an array within a class:

Array points2 { { {1,2},{3,4},{5,6}}};
              ^ ^ ^
              | | |
              | | array element
              | array
              class
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
7

You actually need one more set of braces like so:

Array points2 {{{1,2},{3,4},{5,6}}};

Working example

See this post for further explanation of when these extra braces are required. It is related to whether the container is an aggregate or not.

Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218