如何在C ++中初始化嵌套结构?

时间:2022-06-08 16:58:10

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.

然后我尝试初始化一个名为player的新创建的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结构并初始化它的成员,如下所示:

Player bob;
bob.r_leg = 1;

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

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

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

我搜索了问题,但我没有找到任何文章向我展示如何在(父)结构中初始化嵌套结构的许多不同成员。任何关于这个主题的帮助将不胜感激:-) !!!

3 个解决方案

#1


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

如果您只想初始化点的x成员,可以省略其他初始值设定项。聚合(数组,结构)中的剩余元素将被初始化为“正确”值 - 因此,指针为NULL,bool为false,int为零等,粗略地使用构造函数用于用户定义的类型。初始化点的行看起来像这样

{ { 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.

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

#2


You can add default values to a structure like so:

您可以将默认值添加到结构中,如下所示:

struct Point{
   Point() : x(0), y(0)
     { };

   float x;
   float y;
};

or

struct Point{
   Point() 
     { 
       x = 0;
       y = 0;
     };

   float x;
   float y;
};

For adding those values during construction, add parameters to constructors like this:

要在构造期间添加这些值,请向构造函数添加参数,如下所示:

struct Point{
   Point(float x, float y) 
     { 
       this->x = x;
       this->y = y;
     };

   float x;
   float y;
};

and instantiate them with these:

并用以下方法实例化它们:

Point Pos(10, 10);
Point *Pos = new Point(10, 10);

This also works for your other data structures.

这也适用于您的其他数据结构。

#3


It looks like the bounding box should just contain floats, not Points?

看起来边界框应该只包含浮点数,而不是点数?

That said this might work:

这说可能会有效:

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

(Untested)...

#1


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

如果您只想初始化点的x成员,可以省略其他初始值设定项。聚合(数组,结构)中的剩余元素将被初始化为“正确”值 - 因此,指针为NULL,bool为false,int为零等,粗略地使用构造函数用于用户定义的类型。初始化点的行看起来像这样

{ { 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.

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

#2


You can add default values to a structure like so:

您可以将默认值添加到结构中,如下所示:

struct Point{
   Point() : x(0), y(0)
     { };

   float x;
   float y;
};

or

struct Point{
   Point() 
     { 
       x = 0;
       y = 0;
     };

   float x;
   float y;
};

For adding those values during construction, add parameters to constructors like this:

要在构造期间添加这些值,请向构造函数添加参数,如下所示:

struct Point{
   Point(float x, float y) 
     { 
       this->x = x;
       this->y = y;
     };

   float x;
   float y;
};

and instantiate them with these:

并用以下方法实例化它们:

Point Pos(10, 10);
Point *Pos = new Point(10, 10);

This also works for your other data structures.

这也适用于您的其他数据结构。

#3


It looks like the bounding box should just contain floats, not Points?

看起来边界框应该只包含浮点数,而不是点数?

That said this might work:

这说可能会有效:

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

(Untested)...