如何初始化C ++中的嵌套结构? [英] How to initialize nested structures in C++?

查看:163
本文介绍了如何初始化C ++中的嵌套结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在程序中创建了几个不同的结构。我现在有一个嵌套结构的结构,但是我无法解决如何正确地初始化它们。结构如下所示。

  / *** POINT STRUCTURE *** / 
struct Point {
float x;点
float y;点
}的// y coord;

/ ***边界框结构*** /
struct BoundingBox {
点ymax,ymin,xmax,xmin;
};

/ *** PLAYER STRUCTURE *** /
struct Player {
vector< float> X; // player xcoords
vector< float> y; //玩家ycoords
BoundingBox框;
浮点红,绿,蓝; //红色,绿色,蓝色颜色值
float r_leg,l_leg; //玩家左右腿的速度
int poly [3]; //每个多边形的点数(3个多边形)
bool up,down;
};

然后我尝试介绍一个新创建的Player结构,称为播放器。

  //创建播放器,使用向量复制和迭代器构造函数
播放器播放器= {
向量< float>(xcords,xcords +(sizeof (xcords)/ sizeof(float)))//播放器
矢量< float>(ycords,ycords +(sizeof(ycords)/ sizeof(float))的xcords),// ycoords of playe
box.ymax = 5; //创建边框
box.ymin = 1;
box.xmax = 5;
box.xmin = 1;
1,1,1,//红色,绿色,蓝色
0.0f,0.0f,// r_leg,l_leg
{4,4,4},//每个多边形的数字点
true,false}; // up,down

这会导致几个不同的错误,包括框。说明框在'。'之前没有明确的标识符和缺少的结构或语法。



然后我尝试创建一个Player结构体,并初始化它的成员如下: p>

 玩家bob; 
bob.r_leg = 1;

但是这会导致更多错误,因为编译器认为bob没有标识符或缺少一些语法。 / p>

我讨厌了这个问题,但我没有找到任何文章告诉我如何在(父)结构中嵌套结构的许多不同成员。任何关于这个问题的帮助将不胜感激:-) !!!

解决方案

您正常使用 {...}

 播放器播放器= {
vector< float> (xcords,xcords +(sizeof(xcords)/ sizeof(float))),
vector< float>(ycords,ycords +(sizeof(ycords)/ sizeof(float))),
5, 1,5,1,5,1,5,1,
1.0f,1.0f,1.0f,//红,绿,蓝
0.0f,0.0f,// r_leg,l_leg
{4,4,4},//每个多边形的数量点
true,false
};

现在,这是使用大括号。一些编译器警告说,即使它是完全标准的,因为它可能会混淆读者。更好的是添加大括号,所以很清楚初始化的位置在哪里:

 播放器播放器= {
vector< float> (xcords,xcords +(sizeof(xcords)/ sizeof(float))),
vector< float>(ycords,ycords +(sizeof(ycords)/ sizeof(float))),
{ 5,1},{5,1},{5,1},{5,1}},
1.0f,1.0f,1.0f,//红,绿,蓝
0.0f ,0.0f,// r_leg,l_leg
{4,4,4},//每个多边形的数字点
true,false
};

如果您只想初始化 x 你可以通过省略另一个初始化器来实现这一点。聚合中的剩余元素(数组,结构体)将被初始化为正确值 - 因此,一个 NULL 用于指针, false 为bool,零为int等,并大致使用用户定义类型的构造函数。初始化点的行看起来像这样,然后

  {{5},{5},{5},{5}} ,

现在,您可以看到使用大括号elision的危险。如果你添加一些成员到你的结构体,所有的初始化器都将它们的实际元素分开,它们应该被初始化,并且它们可能会意外地碰到其他元素。所以你最好总是在适当的时候使用括号。



考虑使用构造函数。我刚刚完成了您的代码,显示如何使用括号初始化程序。


I have created a couple of different structures in a program. I now have a structure with nested structures however I cannot work out how to initialize them correctly. The structures are listed below.

/***POINT STRUCTURE***/
struct Point{
    float x;                    //x coord of point
    float y;                    //y coord of point
};

/***Bounding Box STRUCTURE***/
struct BoundingBox{
    Point ymax, ymin, xmax, xmin;
};

/***PLAYER STRUCTURE***/
struct Player{
    vector<float> x;            //players xcoords
    vector<float> y;            //players ycoords
    BoundingBox box;
    float red,green,blue;       //red, green, blue colour values
    float r_leg, l_leg;         //velocity of players right and left legs
    int poly[3];                //number of points per polygon (3 polygons)
    bool up,down;               
};

I then attempt to intialse a newly created Player struct called player.

//Creates player, usings vectors copy and iterator constructors
Player player = { 
vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ), //xcords of player
vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ), //ycoords of playe
box.ymax = 5;               //create bounding box
box.ymin = 1;
box.xmax = 5;
box.xmin = 1;
1,1,1,                      //red, green, blue
0.0f,0.0f,                  //r_leg,l_leg
{4,4,4},                    //number points per polygon
true,false};                //up, down

This causes several different errors, concerning box. Stating the box has no clear identifier and missing struct or syntax before '.'.

I then tried just to create a Player struct and initialise it's members as follows:

Player bob;
bob.r_leg = 1;

But this causes more errors, as the compiler thinks bob has no identifier or is missing some syntax.

I googled the problem but I did not find any articles showing me how to initalise many different members of nested structures within the (parent) structure. Any help on this subject would be greatly appreciated :-) !!!

解决方案

You initialize it normally with { ... }:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  5, 1, 5, 1, 5, 1, 5, 1, 
  1.0f,1.0f,1.0f,                         //red, green, blue
  0.0f,0.0f,                              //r_leg,l_leg
  {4,4,4},                                //number points per polygon
  true,false
};   

Now, that is using "brace elision". Some compilers warn for that, even though it is completely standard, because it could confuse readers. Better you add braces, so it becomes clear what is initialized where:

Player player = { 
  vector<float>(xcords,xcords + (sizeof(xcords) / sizeof(float)) ),
  vector<float>(ycords,ycords + (sizeof(ycords) / sizeof(float)) ),
  { { 5, 1 }, { 5, 1 }, { 5, 1 }, { 5, 1 } }, 
  1.0f, 1.0f, 1.0f,               //red, green, blue
  0.0f, 0.0f,                     //r_leg,l_leg
  { 4, 4, 4 },                    //number points per polygon
  true, false
};

If you only want to initialize the x member of the points, you can do so by omitting the other initializer. Remaining elements in aggregates (arrays, structs) will be value initialized to the "right" values - so, a NULL for pointers, a false for bool, zero for ints and so on, and using the constructor for user defined types, roughly. The row initializing the points looks like this then

{ { 5 }, { 5 }, { 5 }, { 5 } }, 

Now you could see the danger of using brace elision. If you add some member to your struct, all the initializers are "shifted apart" their actual elements they should initialize, and they could hit other elements accidentally. So you better always use braces where appropriate.

Consider using constructors though. I've just completed your code showing how you would do it using brace enclosed initializers.

这篇关于如何初始化C ++中的嵌套结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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