C++ 不能为数组指定显式初始值设定项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20894398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot specify explicit initializer for arrays
提问by Jimmyt1988
I'm getting the following compile error...
我收到以下编译错误...
error C2536: 'Player::Player::indices' : cannot specify explicit initializer for arrays
why is this?
为什么是这样?
header
标题
class Player
{
public:
Player();
~Player();
float x;
float y;
float z;
float velocity;
const unsigned short indices[ 6 ];
const VertexPositionColor vertices[];
};
cpp
cp
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 ) }
}
{
}
EDIT TO SHOW MY ATTEMPT AT std::array
编辑以在 std::array 显示我的尝试
std::array<unsigned short, 6> indices;
std::array<VertexPositionColor, 4> vertices;
can't get this to work either.
也不能让它工作。
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 ) }
} )
it crashes the compiler...
它使编译器崩溃...
EDIT:: Victory!
编辑:胜利!
I put them in my cpp file babeh:
我把它们放在我的 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 ) }
}
回答by Jimmyt1988
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:
正如其他人所说,将我的类的属性设置为静态常量,然后在类的 cpp 文件中定义它们:
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:
cp:
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 ) }
}
回答by Dietmar Kühl
The size of the array needs to be defined in the class definition. C++ doesn't support variable sized arrays, at least, not yet:
数组的大小需要在类定义中定义。C++ 不支持可变大小的数组,至少现在不支持:
class Player
{
public:
// ...
const unsigned short indices[ 6 ];
const VertexPositionColor vertices[4];
};
Assuming a suitable definition of VertexPositionColor
this should be OK (it compiles with gcc and clang using -std=c++11
).
假设一个合适的定义VertexPositionColor
应该没问题(它使用 gcc 和 clang 编译-std=c++11
)。