defining bi-dimensional std::array

Hello everyone,
I get this error with the below code :
"too many initializers for
‘const std::array<std::array<int, 2>, 5>’ "
[code]
class mydata
{
std::array<std::array<int, 2>, 5> bi_dataM;

public:
void filldataM()
{
const std::array<std::array<int, 2>, 5> localdataLC
{ { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } };

bi_dataM = localdataLC;
}
};
// my "{}" balance ok ?
Hope you can help
Michel Chassey
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <array>

int main()
{
   std::array<std::array<int, 5>, 2> bi_dataM;

   const std::array<std::array<int, 5>, 2> localdataLC
   { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 };

   bi_dataM = localdataLC;

   for (int i = 0; i < 2; i++)
   {
      for (int j = 0; j < 5; j++)
      {
         std::cout << bi_dataM[i][j] << ' ';
      }
      std::cout << '\n';
   }
}

One of the peculiarities of trying to create a 2D STL container is the dimensions have to be reversed on creation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <array>

class mydata
{
    std::array<std::array<int, 2>, 5> bi_dataM;

public:

    void filldataM()
    {
        const std::array<std::array<int, 2>, 5> localdataLC
        { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 }; // brace elision 

        bi_dataM = localdataLC;

        const std::array<std::array<int, 2>, 5> localdataLC2
        { { {{1,2}}, {{3,4}}, {{5,5}}, {{4,3}}, {{2,1}} } }; // fully braced

        bi_dataM = localdataLC2;
    }

    // initialise in constructor (fully braced)
    mydata() : bi_dataM { { {{1,2}}, {{3,4}}, {{5,5}}, {{4,3}}, {{2,1}} } } {}

    // initialise in constructor (brace elision)
    mydata(int) : bi_dataM { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 } {}

};

http://coliru.stacked-crooked.com/a/c80ae4695857c4fa
closed account (E0p9LyTq)
Or add an extra set of outer braces to the initializer list to unconfuse the compiler:

{ { { 1, 2, 3, 4, 5 }, { 5, 4, 3, 2, 1 } } };
That should (and would) generate an error.
The compiler does not get confused about the size of an array if we place an extra pair of braces.
http://coliru.stacked-crooked.com/a/4faca0243d3f6cb7
http://rextester.com/JPOEB8667
closed account (E0p9LyTq)
That should (and would) generate an error.

Using an initializer list with extra outer braces for a 2D std::array is required by Visual C++ 2017, even using the latest ISO C++ standard. std:c++14/c++15/latest.

I am not saying it is correct, but that is what it is.

The extra braces aren't required with initializer lists for a std::vector.
awright people !
I finally got this to compile :
const int rows = 2;
const int columns = 5;
/* const std::array<std::array<int, rows>, columns> dataGC */
const std::array<std::array<int, columns>, rows> dataGC
{ 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 };

Awesome !)
Reverse the dimensions you say ? To a C98 person this is a huge step.
Somewhere on the net is an "orthodox" statement for a
multi-dimensionnal array.

Now I can initialize my bi_dataM member.

Thans all,
Michel Chassey
> Now I can initialize my bi_dataM member.

Initialise it in the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <array>
#include <iostream>

class mydata
{
    std::array< std::array<int,2>, 5 > bi_dataM;

    public:

        // constructor: initialises member bi_dataM
        mydata() : bi_dataM { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1 } {}

        void debug_dump() const
        {
            std::clog << "mydata\n{\n  bi_dataM\n  [\n" ;
            for( const auto& row : bi_dataM )
            {
                std::clog << "    [ " ;
                for( int v : row ) std::clog << v << ' ' ;
                std::clog << "]\n" ;
            }
            std::clog << "  ]\n}\n" ;
        }
};

int main()
{
    const mydata md ;
    md.debug_dump() ;
}

http://coliru.stacked-crooked.com/a/07be69b08cbf86f8
Topic archived. No new replies allowed.