Workaround for Error C2536: Cannot Specify Explicit Initializer for Arrays in Visual Studio 2013

Workaround for error C2536: cannot specify explicit initializer for arrays in Visual Studio 2013

As the comments, you can try this workaround.

class A
{
A() : m_array ({ 0, 1, 2 }) {}
private:
std::array<int, 3> m_array;
};

It seems VS2013 made initializer-list for std::array constructor well and you can initialize it in constructor's intializer. The code that you wrote is valid and both gcc and clang support it. VS2013 lacks.

Can't specify explicit initializer for arrays

From Bjarne's C++11 FAQ page:

In C++98, only static const members of integral types can be initialized in-class, and the initializer has to be a constant expression. [...] The basic idea for C++11 is to allow a non-static data member to be initialized where it is declared (in its class).

The problem is, VS2013 doesn't implement all the features of C++11, and this is one of them. So what I suggest you use is std::array (take note of the extra set of braces):

#include <array>

class A
{
public:
A() : a({ { 1, 2, 3 } }) {} // This is aggregate initialization, see main() for another example

private:
std::array<int, 3> a; // This could also be std::vector<int> depending on what you need.
};

int main()
{
std::array<int, 3> std_ar2 { {1,2,3} };
A a;

return 0;
}

cppreference link on aggregate initialization

If you're interested you can click on this link to see that what you did does compile when using a compiler that has implemented this feature (in this case g++, I've tried it on clang++ and it works too).

VSC13 - cannot specify explicit initializer for arrays / for STRINGS

As you wrote it won't compile because you can't initialize a non-static array within the definition. This works though:

#include <array>
class a{
public:
a() : words({"cake","pie","steak"})
{
}

std::array<std::string, 3> words;
};

cannot specify explicit initializer for arrays

As everyone else was saying, set the properties of my class to static const and then define them in the cpp file for the class:

header file:

class Player
{
public:
Player();
~Player();

float x;
float y;
float z;
float velocity;

static const unsigned short indices[ 6 ];
static const VertexPositionColor vertices[ 4 ];
};

cpp:

const unsigned short Player::indices[ 6 ] = {
3, 1, 0,
4, 2, 1
};

const VertexPositionColor Player::vertices[ 4 ] = {
{ XMFLOAT3( -0.5f, -0.5f, -0.5f ), XMFLOAT3( 0.0f, 0.0f, 0.0f ) },
{ XMFLOAT3( -0.5f, 0.5f, -0.5f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 0.5f, -0.5f, -0.5f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },
{ XMFLOAT3( 0.5f, 0.5f, -0.5f ), XMFLOAT3( 0.0f, 1.0f, 1.0f ) }
}

error C2536: : cannot specify explicit initializer for arrays

In a "natural" context (i.e. an array object declaration) you declaration with initialization is perfectly valid. There's nothing wrong with it.

The only explanation for the error is that, as you noted in the comments, your array is declared as a struct member and you are trying to specify the initializer right in the struct declaration. Doing something like that just does not make any sense in C. You cannot specify initializers for members in struct declarations, regardless of whether these members are arrays or something else.

Initializers are supposed to be specified in object definitions, as in

struct S 
{
unsigned long z[8];
};

struct S s = { { 0xffffffff, 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0xffffffff, 0xffffffff, 0xffffffff } };

C2660: _splitpath_s fails with std::array error in Visual Studio 2013

The 5 parameter version of _splitpath_s is a template function, expecting a character pointer for the input path, and C-style character arrays for the other 4. Since you are passing in C++ array objects, the template is not generated and, due to SFINAE it is not available so there is no function that takes 5 parameters.

To use it you'll have to use the 9 parameter version, where you pass in the input addresses and buffer sizes.

Initialize a two dimensional array in C++

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.



Related Topics



Leave a reply



Submit