cannot specify explicit initializer for arrays(不能为数组指定显式初始值设定项)
问题描述
我收到以下编译错误...
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 ) }
}
这篇关于不能为数组指定显式初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不能为数组指定显式初始值设定项
- 如何对自定义类的向量使用std::find()? 2022-11-07
- STL 中有 dereference_iterator 吗? 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 静态初始化顺序失败 2022-01-01
- 从python回调到c++的选项 2022-11-16