accessing variables in classes

I'm trying to get the x and y's value from a class and theres classes defined within classes and I can't figure out how to get the value from them.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class rectangle
{
    private:

    int x,y,brx,bry,w,h;

    public:

    rectangle(int xpos , int ypos , int width , int height): 
    x(xpos), y(ypos), brx(xpos + width - 1), bry(ypos + height - 1),
    w(width), h(height){}

    inline bool Overlaps(const rectangle& r);
    int X() {return x;}
    int Y() {return y;}
    int BRX() {return brx;}
    int BRY() {return bry;}
    int W() {return w;}
    int H() {return h;}
};

    inline bool rectangle::Overlaps(const rectangle& r)
    {
        return !(((x > r.brx) || (brx < r.x)) || ((y > r.bry) || (bry < r.y)));
    }

class Box
{
    public:

    rectangle area;
    ALLEGRO_BITMAP* Image;
    bool IsBroken;

     Box(int xpos , int ypos , int width , int height , ALLEGRO_BITMAP* Image):
     area(xpos,ypos,width,height), Image(Image), IsBroken(false){}
};


class Level
{
    public:

    vector<Box> boxes;
};

Level LevelOne;


I enter the data like this:

 
LevelOne.boxes.push_back(Box(Xincrement, Yincrement, 40, 20, GreenBox));


here I'm declaring an instance of rectangle in a local scope and giving it the values of the current x and y

 
rectangle box(LevelOne.boxes.area.X()[i] , LevelOne.boxes.area.Y()[i] , 40 , 20);

but it says boxes does not have a member named area when I clearly defined it. I'm using classes a lot more in this and its kind of confusing me so any help would be greatly appreciated
Last edited on
boxes is a vector, move the subscript operator after it: LevelOne.boxes[i].area.X()
PS: That Box uses rectangles is an implementation detail - it should IMO be private.
boxes is a vector, move the subscript operator after it: LevelOne.boxes[i].area.X()


Ha! worked perfect.
Topic archived. No new replies allowed.