C++ returning struct values from functions

  • A+

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.

 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

I just do not understand how to pass the data from the function into the varable in main.


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.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: