Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression must have pointer-to-class-type

I have a struct "MachineState" and i created a list of type "MachineState*". When i Try to iterate through the list i Keep getting "

error C2839: invalid return type 'MachineState **' for overloaded 'operator ->

I'm using Microsoft Visual Studio 10. I googled the error and all i could find out was "The -> operator must return a class, struct, or union, or a reference to one."

Struct MachineState
{

   template <typename MachineTraits>
   friend class Machine;

   enum Facing { UP, RIGHT, DOWN, LEFT};
   MachineState()
    : m_ProgramCounter(1)
    , m_ActionsTaken(0)
    , m_Facing(UP)
    , m_Test(false)
    , m_Memory(nullptr)
    ,x(0)
    ,y(0)
    ,point1(25, 10)
    ,point2(10, 40)
    ,point3(40, 40)

   { }


   int m_ProgramCounter;
   int m_ActionsTaken;

   Facing m_Facing;
    bool m_Test;
    int x;
    int y;
    Point point1;
    Point point2;
    Point point3;

};

I declare the list as

 std::list<MachineState*> zombs;

Here is where I try to iterate through my list and i keep getting the error, on the "it->point1" saying that the expression must have a pointer to class type.

    for(std::list<MachineState*>::iterator it = zombs.begin(); it != zombs.end(); it++)
     {
        Point points[3] = {it->point1, it->point2, it->point3};
        Point* pPoints = points;
        SolidBrush brush(Color(255, 255, 0, 0));
        m_GraphicsImage.FillPolygon(&brush, pPoints,3);
     }

If anyone can explain me what's wron

like image 814
John Kemp Avatar asked Apr 24 '13 04:04

John Kemp


1 Answers

it is an iterator to a pointer to a MachineState.

You need to dereference the iterator and then the pointer.

Point points[3] = {(*it)->point1, (*it)->point2, (*it)->point3};

Edit:

Dereferencing means getting the thing that it's referring to.

Dereferencing is done with the * or -> operator.

If it were a MachineState, you could use it.point1

If it were a pointer to a MachineState, you could use it->point1 or (*it).point1

If it were a iterator to a MachineState, you could also use it->point1 or (*it).point1

Since it is an iterator to a pointer to a MachineState, you must use (*it)->point1 or (**it).point1

like image 197
Drew Dormann Avatar answered Sep 25 '22 21:09

Drew Dormann