简体   繁体   English

C ++从函数返回struct值

[英]C++ returning struct values from functions

So for one of my assignments i have to generate random graphic circles and rectangles, using structures. 因此,对于我的一个任务,我必须使用结构生成随机图形圆和矩形。 but i cannot fathom how to get the structure to output from a functions. 但我无法理解如何从一个函数输出结构。

   struct Circle{
   int x;
     int y;
     int radius;
     int r;
     int g;
     int b;
   };

   Circle createCirc() {
     int x = rand() % window_Width;
     int y = rand() % window_Height;
     int radius = rand() % 100;
     int r = rand()%256;
     int g = rand()%256;
     int b = rand()%256;
     return Circle(x,y,radius,r,g,b);
   }

here i create the struct with basic values for the object, then i pass some data from main into this function. 在这里我用对象的基本值创建结构,然后我将一些数据从main传递给这个函数。

 Circle circle[1000];
 circle[count] = createCirc();

however i cannot even get it to run as apparently when defining the struct itself it comes with this error: 但是,我甚至无法让它运行,因为显然在定义结构本身时它带有这个错误:

main.cpp:47:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 6 were provided main.cpp:47:8:注意:候选构造函数(隐式移动构造函数)不可行:需要1个参数,但是提供了6个

I just do not understand how to pass the data from the function into the varable in main. 我只是不明白如何将函数中的数据传递给main中的varable。

You can use 您可以使用

 return Circle(x,y,radius,r,g,b);

only when there is an explicitly defined constructor that takes those arguments. 只有当有明确定义的构造函数接受这些参数时。 Change it to: 将其更改为:

 return {x,y,radius,r,g,b};

The second form uses aggregate initialization to construct a Circle . 第二种形式使用聚合初始化来构造Circle

struct Circle {
    int x;
    int y;
    int radius;
    int r;
    int g;
    int b;
};

You are only defining fields to the class, but not a constructor. 您只是为类定义字段,而不是构造函数。

The () initialization syntax does not allow to do what you're doing. ()初始化语法不允许执行您正在执行的操作。
However, C++11 aggregate-initialization can, as pointed out by @RSahu 's post. 但是,正如@RSahu的帖子所指出的, C ++ 11聚合初始化可以。

An alternative is to define a constructor to your class, optionally using the member-initialization list (see a few reasons here why to). 另一种方法是定义构造函数到类,可以选择使用成员初始化列表(见的几个原因这里为什么)。

Essentially, it would do what your createCirc function is attempting to do. 从本质上讲,它会执行createCirc函数尝试执行的操作。 You would define it as such: 你可以这样定义它:

struct Circle {
    int x, y, radius, r, g, b;

    Circle();
};

Circle::Circle() :
    x{rand() % window_width},
    y{rand() % window_height},
    radius{rand() % 100},
    r{rand() % 256},
    g{rand() % 256},
    b{rand() % 256}
{}

This would allow you to do something like Circle myCircle; 这将允许你做像Circle myCircle;这样的事情Circle myCircle; and it will get initialized as you expect. 它会像你期望的那样初始化。

That being said, a constructor is not necessarily the best way to do this (IMO it hides too much behavior here) but it's a good thing to know. 话虽这么说,构造函数不一定是最好的方法(IMO它隐藏了太多的行为),但这是一件好事。

Just create a new Circle inside your createCirc() function and return it: 只需在createCirc()函数中创建一个新的Circle并将其返回:

Circle createCirc() {
    Circle circle;
    circle.x = rand() % window_Width;
    circle.y = rand() % window_Height;
    circle.radius = rand() % 100;
    circle.r = rand()%256;
    circle.g = rand()%256;
    circle.b = rand()%256;
    return circle;
}

Also you should think about using a vector for dynamic allocation and storage. 您还应该考虑使用vector进行动态分配和存储。

The following should work nicely. 以下应该很好地工作。

struct Circle
{
    Circle(); // the default constructor
    int x;
    int y;
    int radius;
    int r;
    int g;
    int b;
};

Circle::Circle() : // start member-initialization list
   x( rand() % window_Width ),
   y( rand() % window_Height ),
   radius( rand() % 100 ),
   r( rand()%256 ),
   g( rand()%256 ),
   b( rand()%256 )
{
    // nothing to do!
}

I choose this implementation for a couple of reasons. 我选择此实现有几个原因。 The main reason is that you want to construct 1000 of these and store them in an array. 主要原因是您要构造其中的1000个并将它们存储在数组中。 When an array of objects is created (assuming it isn't an array of int or other plain data type) the default constructor will be called for each element. 创建对象数组时(假设它不是int数组或其他普通数据类型),将为每个元素调用默认构造函数 That's what we defined here. 这就是我们在这里定义的内容。 And by using the member-initialization list we get those initial values into the member variables more efficiently than we would by assigning them in the body of the constructor. 通过使用成员初始化列表,我们将这些初始值更有效地提取到成员变量中,而不是将它们分配到构造函数的主体中。 That isn't terribly important for int values, but it's a good habit to get into. 这对int值来说并不是非常重要,但这是一个很好的习惯。

Note that if you wanted to construct Circles with other values, it would be a waste of time to use this default constructor. 请注意,如果您想用其他值构造Circles ,那么使用此默认构造函数将浪费时间。 You'd want to define another constructor that takes parameters for size, position, and/or RGB values. 您需要定义另一个构造函数,该构造函数接受大小,位置和/或RGB值的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM