2

I'm learning C++ and I have a problem. I have this declaration in a class header file:

double bgal[3][3] =
    { { -0.066988739415,-0.872755765852,-0.483538914632 },
    { 0.492728466075,-0.450346958020, 0.744584633283 },
    { -0.867600811151,-0.188374601723, 0.460199784784 } };

With Visual Studio 2015 compiles fine, but with Visual Studio 2013 it doesn't compile. I get this message:

cannot specify explicit initializer for arrays

I think the problem is related that Visual Studio 2013 doesn't support C++11 and Compiler Error C2536.

I have tried to move that initialization inside the class constructor, but it doesn't work. This:

MyClass::MyClass() : bgal { { -0.066988739415, -0.872755765852, -0.483538914632 }, 
                            { 0.492728466075, -0.450346958020, 0.744584633283 },
                            { -0.867600811151, -0.188374601723, 0.460199784784 } }

But it doesn't work.

Any advice? Maybe I can't make this vector constant or static, or...

I have tried bgal[0][0] = { ...}; bgal[0][1] = { ...}; but it is a lot of work.

It is not a duplicate of question Error: cannot specify explicit initializer for array because that question asks about a one dimension array, and it offers as a solution to initialize the array with bgal[0][0] = { ...}; bgal[0][1] = { ...}; that it is a lot of work. And I asking if there is another way to do it faster.

Please read the question carefully before looking for possible duplicate questions.

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    Don't you have to set a compiler setting explicitly for VS2013 to enable C++11? – Mr Lister Oct 24 '15 at 15:47
  • @BartoszPrzybylski Please, read my update. I have found that question before ask mine, and its solution is on my question, and I'm asking if there is another solution. – VansFannel Oct 24 '15 at 15:54
  • @MrLister I'm going to use this lib with Unreal Engine and I'm not sure if I can enable C++ 11. I will check it. As Microsoft said, I don't have to do anything special to enable C++ 11 features in Visual Studio 2013, so it doesn't support explicit array initialization. – VansFannel Oct 24 '15 at 15:57
  • @VansFannel There is also this question: http://stackoverflow.com/questions/19877757/workaround-for-error-c2536-cannot-specify-explicit-initializer-for-arrays-in-vi which seems to point out that this is a VS bug O_o – Bartosz Przybylski Oct 24 '15 at 16:00
  • @BartoszPrzybylski Is std::array equivalent to double bgal[3][3] array? – VansFannel Oct 24 '15 at 16:07
  • @MrLister no, you don't – OMGtechy Oct 24 '15 at 17:19
  • Tell Microsoft to give you a real C compiler. – clearlight Oct 25 '15 at 06:55

2 Answers2

3

Continuing discussion from comments, and according to this question: Workaround for error C2536: cannot specify explicit initializer for arrays in Visual Studio 2013

you can use something like this, altho its not as pretty as raw array or arrays

array<array<double, 3>, 3> a({
    array<double,3>({ -0.066988739415,-0.872755765852,-0.483538914632 }),
    array<double,3>({ 0.492728466075,-0.450346958020, 0.744584633283 }),
    array<double,3>( { -0.867600811151,-0.188374601723, 0.460199784784 }) });

To make it a little prettier you can always define a helper macro

#define DD array<double,3>

array<array<double, 3>, 3> a({
    DD({ -0.066988739415,-0.872755765852,-0.483538914632 }),
    DD({ 0.492728466075,-0.450346958020, 0.744584633283 }),
    DD( { -0.867600811151,-0.188374601723, 0.460199784784 }) });
#undef DD

You can always try to use vector of vectors with initialized list like this: https://ideone.com/lQ12a4

vector<vector<double> > a{
    { -0.066988739415,-0.872755765852,-0.483538914632 },
    { 0.492728466075,-0.450346958020, 0.744584633283 },
    { -0.867600811151,-0.188374601723, 0.460199784784 } };

I don't know if this will be working on your compiler but according to this: https://msdn.microsoft.com/en-US/library/hh567368.aspx it should.

Community
  • 1
  • 1
Bartosz Przybylski
  • 1,628
  • 10
  • 15
0

Here is a demonstrative program that shows the data member initialization of an array type. You can select the method that allows to initialize the array using your compiler.:)

#include <iostream>

struct A
{
    double bgal[3][3] 
    { { -0.066988739415,-0.872755765852,-0.483538914632 },
      { 0.492728466075,-0.450346958020, 0.744584633283 },
      { -0.867600811151,-0.188374601723, 0.460199784784 } 
    };    
};

struct B
{
    B() : bgal { { -0.066988739415,-0.872755765852,-0.483538914632 },
                 { 0.492728466075,-0.450346958020, 0.744584633283 },
                 { -0.867600811151,-0.188374601723, 0.460199784784 } }
    {

    }

    double bgal[3][3];
};

int main()
{
    A a;
    B b;

    for ( const auto &row : a.bgal )
    {
        for ( double x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    std::cout << std::endl;

    for ( const auto &row : b.bgal )
    {
        for ( double x : row ) std::cout << x << ' ';
        std::cout << std::endl;
    }
}

The program output is

-0.0669887 -0.872756 -0.483539 
0.492728 -0.450347 0.744585 
-0.867601 -0.188375 0.4602 

-0.0669887 -0.872756 -0.483539 
0.492728 -0.450347 0.744585 
-0.867601 -0.188375 0.4602 

It was compiled with the on-line MS compiler

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335