Then in my Class B I create a vector of objects of Class A and try to retrieve contents created in A
Class B.h
1 2 3 4 5 6 7
Class B {
private:
vector <Class A> vectorB;
public:
Class B();
virtual ~Class B();
};
Class B.cpp
1 2 3 4 5 6 7 8 9 10
Class B::Class B() {
Class A a ;
vectorB = a.getVector();
cout << vectorB[0].getValue() << endl;
}
Class B::~Class B() {
}
When I try to print the first element of the vector to see if I accessing its content, my program falls, it does not give an error, but debugging says it could not access to that value. The first couple of times worked and printed the value, but later started to fail.
It's hard to say what the problem is when we don't see any real code.
Note that you are copying the vector on line 3 in "Class B.cpp". A::vector and B::vectorB are two different vectors so modifying one of them will have no effect on the other.
The variable a is a local variable inside the constructor so it will not exist after the constructor has ended, so I hope you are not keeping any references to a or to the vector returned by a.getVector().
I created a instance from Class A in the main and call the functions to populate the vector and print it, in this way the vector is retrieved when I called from the Main.
The second way to get it work, but from class B, like the original, is call constructor of Class A in the Main, inside that constructor are the calls to the methods for fill and print the vector.