c++ – cannot specify explicit initializer for arrays

c++ – 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:

n

header file:

n

class Playern{npublic:n    Player();n    ~Player();nn    float x;n    float y;n    float z;n    float velocity;nn    static const unsigned short indices[ 6 ];n    static const VertexPositionColor vertices[ 4 ];n};n

n

cpp:

n

const unsigned short Player::indices[ 6 ] = {n    3, 1, 0,n    4, 2, 1n};nnconst VertexPositionColor Player::vertices[ 4 ] = {n    { XMFLOAT3( -0.5f, -0.5f, -0.5f ), XMFLOAT3( 0.0f, 0.0f, 0.0f ) },n    { XMFLOAT3( -0.5f, 0.5f, -0.5f ), XMFLOAT3( 0.0f, 0.0f, 1.0f ) },n    { XMFLOAT3( 0.5f, -0.5f, -0.5f ), XMFLOAT3( 0.0f, 1.0f, 0.0f ) },n    { XMFLOAT3( 0.5f, 0.5f, -0.5f ), XMFLOAT3( 0.0f, 1.0f, 1.0f ) }n}n

The size of the array needs to be defined in the class definition. C++ doesnt support variable sized arrays, at least, not yet:

n

class Playern{  npublic:n    // ...n    const unsigned short indices[ 6 ];n    const VertexPositionColor vertices[4];n};n

n

Assuming a suitable definition of VertexPositionColor this should be OK (it compiles with gcc and clang using -std=c++11).

c++ – cannot specify explicit initializer for arrays

Leave a Reply

Your email address will not be published.