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.

7
  • 1
    Don't you have to set a compiler setting explicitly for VS2013 to enable C++11? Commented Oct 24, 2015 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. Commented Oct 24, 2015 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. Commented Oct 24, 2015 at 15:57
  • @VansFannel There is also this question: stackoverflow.com/questions/19877757/… which seems to point out that this is a VS bug O_o Commented Oct 24, 2015 at 16:00
  • @BartoszPrzybylski Is std::array equivalent to double bgal[3][3] array? Commented Oct 24, 2015 at 16:07

2 Answers 2

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.

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

14 Comments

Is that array equivalent to bgal[3][3]? In my code I'm using for (i = 0; i < 3; i++) { pos1[i] = pos[0] * bgal[i][0] + pos[1] * bgal[i][1] + pos[2] * bgal[i][2]; }
It is equivalent, your code should work because std::array has correctyle defined operator[], but you won't be able to cast directly to double[][]
I have used your code in my header file and I getting a lot of error. Where do I have to use your code?
@VansFannel What sort of error? to use std::array you need to include <array> header
at a({ <- type specifier he expected. At std::array<double, 3>({ <-- error expression expected. And at }) }); <-- ; expected.
|
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

11 Comments

But isn't online compiler using vs 2015?
If I do what are you suggesting in struct B I get the same error message.
@VansFannel Are you show that you copied and pasted my example?
@Bartosz Przybylski The example allows to check whether a particular compiler will compile the program.
@VladfromMoscow could you post a link to this compiler?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.