不能为数组指定显式初始值设定项
我收到以下编译错误...
I'm getting the following compile error...
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
如果我像另一篇文章所说的那样在我的构造中这样做:
and if I do this in my construct like the other post says:
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 中:
I put them in my cpp file 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 ) }
}
推荐答案
正如其他人所说,将我的类的属性设置为静态常量,然后在类的 cpp 文件中定义它们:
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:
头文件:
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 ) }
}
相关文章