不能为数组指定显式初始化程序 [英] cannot specify explicit initializer for arrays

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

问题描述

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

 错误C2536:'Player :: Player :: indices' :不能为数组指定显式初始化器

为什么?



  class Player 
{
public:
Player();
〜Player();

float x;
float y;
float z;
float velocity;

const unsigned short indices [6];
const VertexPositionColor vertices [];
};

cpp

  Player :: Player()

索引
{
3,1,0,
4, 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 f,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< unsigned short,6>指数; 
std :: array< VertexPositionColor,4>顶点;

无法让此工作。

 错误C2661:'std :: array< unsigned short,6> :: array':没有重载函数需要6个参数
pre>

如果我在我的结构中这样做,像其他帖子说:

  indices({
3,1,0,
4,2,1
}),
vertices({
{XMFLOAT3 ,-0.5f,-0.5f),XMFLOAT3(0.0f,0.0f,0.0f)},
{XMFLOAT3(-0.5f,0.5f,-0.5f),XMFLOAT3(0.0f,0.0f,
{XMFLOAT3(0.5f,-0.5f,-0.5f),XMFLOAT3(0.0f,1.0f,0.0f)}, -0.5f),XMFLOAT3(0.0f,1.0f,1.0f)}
})

it crash the compiler ...



EDIT :: Victory! >我把它们放在我的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, f),XMFLOAT3(0.0f,1.0f,0.0f)},
{XMFLOAT3(0.5f,0.5f,-0.5f),XMFLOAT3 }


解决方案

像其他人一样,我的类到静态const,然后在类的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, f),XMFLOAT3(0.0f,1.0f,0.0f)},
{XMFLOAT3(0.5f,0.5f,-0.5f),XMFLOAT3(0.0f,1.0f,1.0f)}
}


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

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<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:

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

解决方案

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:

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:

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天全站免登陆