不能为数组指定显式初始值设定项 [英] cannot specify explicit initializer for arrays

查看:23
本文介绍了不能为数组指定显式初始值设定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ) }
}

这篇关于不能为数组指定显式初始值设定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆