我收到以下编译错误...
error C2536: 'Player::Player::indices' : cannot specify explicit initializer for arrays
为什么会这样?
标题
class Player
{
public:
Player();
~Player();
float x;
float y;
float z;
float velocity;
const unsigned short indices[ 6 ];
const VertexPositionColor vertices[];
};
CPP
Player::Player()
:
indices
{
3, 1, 0,
4, 2, 1
},
vertices{
{ 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 ) }
}
{
}
编辑在std :: array
上显示我的尝试std::array<unsigned short, 6> indices;
std::array<VertexPositionColor, 4> vertices;
也无法使其发挥作用。
error C2661: 'std::array<unsigned short,6>::array' : no overloaded function takes 6 arguments
如果我在我的构造中这样做,就像其他帖子所说:
indices( {
3, 1, 0,
4, 2, 1
} ),
vertices ( {
{ 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 ) }
} )
它崩溃了编译器......
编辑::胜利!
我把它们放在我的cpp文件中:babeh:
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 ) }
}
答案 0 :(得分:11)
正如其他人所说的那样,将我的类的属性设置为static const,然后在类的cpp文件中定义它们:
标题文件:
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 ) }
}
答案 1 :(得分:1)
需要在类定义中定义数组的大小。 C ++不支持可变大小的数组,至少还不支持:
class Player
{
public:
// ...
const unsigned short indices[ 6 ];
const VertexPositionColor vertices[4];
};
假设VertexPositionColor
的合适定义,这应该没问题(它使用-std=c++11
编译gcc和clang。)