i can't add a struct to an array that reside on another class
How can i do?
Assuming i have an array of a struct and that in my main function i want to pass a sigle element (a struct) of the array to an array that reside into another class
You have a class that contains an array of structs, and an array of structs just within main(). You want to take one struct from the array in main and move it into the array in the class?
If so, just have a public function within the class to take the struct and append it to the array of structs that you have within the class data. If you are using a n STL container, that should be as simple as push_back(struct) or insert(struct).
To get the struct from an array, just simply dereference the array for the particular instance of the structure that you want to add, and pass that to the function:
//-----------------
// main.cpp
//-----------------
struct Object {...};
int main() {
Object obj;
Student s;
s.addObject(obj);
}
//-----------------
// students.cpp
//-----------------
class Student {
Object* objects;
// or
Object objects[5];
// or
std::vector<Object> objects;
};
If so, I would recommend to store the objects as a vector (3rd option), because then the "addObject" function for the student class can simply be implemented as objects.push_back(obj);.