gpt4 book ai didi

c++ - 不能为数组指定显式初始值设定项

转载 作者:可可西里 更新时间:2023-11-01 17:37:00 25 4
gpt4 key购买 nike

我收到以下编译错误...

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com