Insert a struct into an array in another class

Hello again,

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


Thank you
Let me clarify:

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct S {
};

class C {
    public:
        void append(S s) { sv.push_back(s); }

    private:
        std::vector<S> sv;
} ;

int main() {
    std::vector<S> mainArray;

    // initialize data

    C c;
    c.append(mainArray[5]);
    c.append(mainArray[0]);
    // etc.

    return 0;
}


If I got your question wrong, just clarify for us.
Sorry,
i confused a bit the things, misc actionscript ecma script class and c++ class.
Want to clarify that the class reside on a different file.

this is the struct in my main.cpp

1
2
3
4
5
6
7
struct Object{

    string name;
    string colour;
    string _class;

};


in my external class "students", a student can have several objects (a pen, a book) and so on. So i store the object of the student in an array.

Now, what if i want to give to a student another object?

I'm in my main function now, and i want to pass an object that have the struct above to a student

I tried some ways, but haven't good results
So this is what you have?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//-----------------
// 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);.
Last edited on
No, i don't have yet.

I tried to implement, but i've failed.

I have something working in actionscript 3, but i can't get to work on c++ :(
@nt3

Hello,

i sent you a PM

Topic archived. No new replies allowed.