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
  • 45,317
  • 7
  • 109
  • 151
Leopoldo
  • 707
  • 2
  • 8
  • 22

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
  • 242,813
  • 27
  • 432
  • 630
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
  • 107,498
  • 14
  • 145
  • 201