akram1rekik
akram1rekik

Reputation: 153

list iterator of segments error C++

I have this sheet of C++ code:

#include <list>
#include <iostream>
using namespace std;


struct point
{
int x;
int y;
} ;
typedef struct point point;


struct segment
{
point* p1;
point* p2;
} ;
typedef struct segment segment;

list<segment*> liste;


int main()

{
    point A,B,C,D;
    A.x=0;A.y=0;
    B.x=1;B.y=0;
    C.x=0;C.y=1;
    D.x=1;D.y=1;

    segment AB,BC,CD,DA;
    AB.p1=&A;AB.p2=&B;
    BC.p1=&B;BC.p2=&C;
    CD.p1=&C;CD.p2=&D;
    DA.p1=&D;DA.p2=&A;
    liste.push_back(&AB);
    liste.push_back(&BC);
    liste.push_back(&CD);
    liste.push_back(&DA);

     for(list<segment*>::iterator it = liste.begin(); it!=liste.end(); ++it)
    {cout <<"Point1 :(" << *it->p1->x <<endl;

    }
}

the error is :

request for member ‘p1’ in ‘* it.std::_List_iterator<_Tp>::operator->()’, which is of pointer type ‘segment*’ (maybe you meant to use ‘->’ ?)

I can't fix this bug Any help will be greatly thankful^^

Upvotes: 2

Views: 54

Answers (1)

fleebness
fleebness

Reputation: 109

    cout << "Point1 :(" << (*it)->p1->x << endl;

You needed to clarify that you want the -> operator for *it, not *(it->...).

Upvotes: 2

Related Questions