vector<struct> container problem

Jul 15, 2010 at 4:55am
I'm trying to make a vector of a struct I created to store points on the screen. This is my class:

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

// .h
class Sierpinski
{
    struct Point
    {
        float x;
        float y;
    };

    typedef std::vector<Point> Container;
    typedef Container::iterator Iter;
    Container* points;
    Point randomPoint;

    public:
        Sierpinski() { }
        ~Sierpinski();
};

// ....
//.cpp

Sierpinski::Sierpinski()
{
    int screenW = GetWidth();
    int screenH = GetHeight();

    points = new Container();
    randomPoint.x = screenW / 2;
    randomPoint.y = screenH / 2;
    points.push_back(randomPoint);

}


Line points.push_back(randomPoint) gives this error:

C:\Sierpinski\src\Sierpinski.cpp|37|error: request for member `push_back' in `((Sierpinski*)this)->Sierpinski::points', 
which is of non-class type `std::vector<Sierpinski::Point, std::allocator<Sierpinski::Point> >*'|


I tried to change Point to a class instead of struct but still have the same error. I'm using Code Blocks with gcc in Windows 7. What am I doing wrong here?
Jul 15, 2010 at 5:51am
points is a pointer, so you need dereference it first before you can access the information.
Jul 15, 2010 at 11:45am
Thanks for remembering me that firedraco, but it was not that the cause of error. I will try and read more the description of vector<>. Thanks
Jul 15, 2010 at 11:49am
Thanks for remembering me that firedraco, but it was not that the cause of error.

Actually, it was.
The problem has nothing to do with vectors, you should read more about pointers first.
Jul 15, 2010 at 11:58am
using works fine here fore me, like firedraco said, it's a pointer..

points->push_back(randomPoint);
Jul 15, 2010 at 12:00pm

Actually, it was.
The problem has nothing to do with vectors, you should read more about pointers first.


To dereference the pointer I should write this, I suppose,
*points

I did, and the error persisted. I will read again about the topics. Thanks for the help.
Jul 15, 2010 at 12:07pm
Try (*points).push_back(randomPoint);

Moooce's suggestion will also work, as x-> is short way to say (*x).
Jul 15, 2010 at 7:38pm
Thanks guys, coming from languages where there´s only reference really complicates things.
Jul 15, 2010 at 8:30pm
I don't see the reason for dynamically allocating the vector. There is no point. Just make it a class attribute so that you don't have a memory leak.

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
class Sierpinski
{
    struct Point
    {
        float x;
        float y;
    };

    typedef std::vector<Point> Container;
    typedef Container::iterator Iter;
    Container points;
    Point randomPoint;

    public:
        Sierpinski() { }
        ~Sierpinski();
};

// ....
//.cpp

Sierpinski::Sierpinski()
{
    int screenW = GetWidth();
    int screenH = GetHeight();

    //points = new Container(); - not necessary anymore
    randomPoint.x = screenW / 2;
    randomPoint.y = screenH / 2;
    points.push_back(randomPoint);

}
Topic archived. No new replies allowed.