Variable Value not recognized outside of Class

Im currently working on creating a game engine and I'm having a bit of trouble with one thing, would love some fresh eyes on this problem, I'll give an example. It's semi-psuedo code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Class_A
{
public:
    void fill()
    {
        vec.pushback(5);
        vec.pushback(6);
    }
    std::vector<int> vec;
}


#include Class_A

Class_B
{
public:
   Class_A object;
   void check()
   {
      std::cout << object.vec[0] << std::endl; // Gives me out of range, because 
      // the size of the vec is 0 from this class. Why cant how can I get around  
      // the problem?
   }
}


Edit: Class A fill is being called before Class B check. I've even printed out the size at the end of the function fill() and it says 2.
Last edited on
Are you calling fill() on the same object?


Something like this should work.

1
2
3
Class_B b;
b.object.fill();
b.check();


Something like this will not work because a is not the same object as b.object.

1
2
3
4
Class_A a;
a.fill();
Class_B b;
b.check();
Last edited on
That makes sense, I tried something and what you said was the problem, just have to find a way to change things so it's implemented correctly. Thank you.
Last edited on
Topic archived. No new replies allowed.