3

When I try to declare a multidimensional array with:

array<array<int, 7>, 5> arrayOne = {
{1, 5, 8, 0, 0, 0, 0},
{2, 3, 8, 7, 7, 0, 0},
{3, 4, 8, 2, 9, 0, 0},
{4, 8, 7, 1, 4, 0, 0},
{5, 7, 6, 8, 3, 0, 0} };

I get:

|10|error: too many initializers for 'std::array<std::array<int, 7u>, 5u>'

But when I do the same with a standard [] array:

int arrayTwo[5][7]= {
{1, 5, 8, 0, 0, 0, 0},
{2, 3, 8, 7, 7, 0, 0},
{3, 4, 8, 2, 9, 0, 0},
{4, 8, 7, 1, 4, 0, 0},
{5, 7, 6, 8, 3, 0, 0} };

I get no errors. I am using mingw g++ on Windows 7 x64. I am new to c++ and stackoverflow, your patience is appreciated.

3
  • Did you want to use extra braces {} to deduce the std::initializer_list? Commented May 30, 2016 at 22:56
  • @πάνταῥεῖ I didn't think I was using any extra braces, can you clarify where exactly they are? Commented May 30, 2016 at 22:58
  • "can you clarify where exactly they are? " Did in my answer. Commented May 30, 2016 at 23:15

1 Answer 1

7

For initialization std::array slightly differs from raw arrays. std::array needs to see aggregate initialization.

You have to put extra braces that the initializer value can be deduced to a std::initializer_list:

#include <array>

int main()
{
    std::array<std::array<int, 7>, 5> arrayOne = {
        { 
     // ^
          {1, 5, 8, 0, 0, 0, 0},
          {2, 3, 8, 7, 7, 0, 0},
          {3, 4, 8, 2, 9, 0, 0},
          {4, 8, 7, 1, 4, 0, 0},
          {5, 7, 6, 8, 3, 0, 0} 
        }
     // ^
    };    
}

See Live Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.