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

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

问题描述

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

  / ***点结构*** / 
struct Point {
float x; // x coord of point
float y; // y coord of point
};

/ ***边界框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 color values
float r_leg,l_leg; // player of players right and left legs
int poly [3]; //每个多边形的点数(3个多边形)
bool up,down;
};

然后我尝试指向一个新创建的Player结构,名为player。

  //创建播放器,使用向量复制和迭代器构造函数
Player player = {
vector< float>(xcords,xcords + (ycords,ycords +(sizeof(ycords)/ sizeof(float))),//播放器的ycoords $ b $(xcords)/ sizeof b box.ymax = 5; //创建边界框
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},//每个多边形的点数
true,false}; // up,down

这会导致几个不同的错误,



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

p>

 玩家鲍勃
bob.r_leg = 1;

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

我googled的问题,但我没有找到任何文章显示我如何在(父)结构中嵌套结构的许多不同的成员。任何关于这个主题的帮助将非常感谢:-) !!!

解决方案

正常初始化 {...}

 玩家player = {
vector< float& (xcords,xcords +(sizeof(xcords)/ sizeof(float))),
向量< 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},//每点的数字点数
true,false
};

现在,使用brace elision。一些编译器警告,即使它是完全标准的,因为它可能混淆读者。更好的方法是添加大括号,因此可以清楚地看到初始化位置:

 玩家= {
vector& (xcords,xcords +(sizeof(xcords)/ sizeof(float))),
向量< float>(ycords,ycords +(sizeof(ycords)/ sizeof 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},//每点的数字点数
true,false
};

如果只想初始化 x 成员的点,你可以通过省略其他初始值设定。聚合中的剩余元素(数组,结构)将被初始化为正确值的值 - 因此,对于指针 NULL code>用于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天全站免登陆