C++ 无法为数组指定显式初始值设定项
我得到以下编译错误C++ 无法为数组指定显式初始值设定项,c++,visual-studio-2013,C++,Visual Studio 2013,我得到以下编译错误 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 in
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;
它使编译器崩溃
编辑::胜利强>
我把它们放在我的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 ) }
}
数组的大小需要在类定义中定义。C++不支持可变大小的数组,至少现在还没有:
class Player
{
public:
// ...
const unsigned short indices[ 6 ];
const VertexPositionColor vertices[4];
};
假设对
VertexPositionColor
有一个合适的定义,这应该没问题(它使用-std=c++11
与gcc和clang一起编译)。正如其他人所说,将我的类的属性设置为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 ];
};
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:
class Player
{
public:
Player();
~Player();
float x;
float y;
float z;
float velocity;
static const unsigned short indices[ 6 ];
static const VertexPositionColor vertices[ 4 ];
};
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 ) }
}
你在用什么编译器?我就知道。。。vs2013。。。这就是问题所在,不是吗。我该怎么办,grr。表示当“使用常量说明符声明的非静态数组。此类数组无法显式初始化”时,您将得到此结果。之前这里已经询问过这一问题,并建议解决方法:如果我去掉常量,这也不起作用。。。同样的错误。我在vs2013 for windows 8 msdn站点上的建议是使成员
静态常量
,因为您正在硬编码它们。问题是vs2013。