作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我收到以下编译错误...
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 ) }
}
最佳答案
正如其他人所说,将我的类的属性设置为静态常量,然后在类的 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 ) }
}
关于c++ - 不能为数组指定显式初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20894398/
我是一名优秀的程序员,十分优秀!