Difficulty accessing a member of a class from a list
Posted: Wed Dec 24, 2008 4:12 pm
So I have a stageObject class...
And just fyi, a hector class with inherits from it...
Now, I have a stage class that contains a list and list interator for stageObjects...
When that class is constructed it makes a hector and puts it in the list...
EDIT: ^^ btw, does anyone know of a nicer way of doing that so it's all on one line... and not needing of dude?
Now, in my main I make a new stage and try to iterate through the list and assign a value to one of the stageObject's member variables...
It iterates through the list just fine. However, the act of trying to access that x member gives me this error:
I have a feeling it's something simple I'm missing... Any help would be appreciated.
Code: Select all
#ifndef STAGEOBJECT_H
#define STAGEOBJECT_H
#include <GL/gl.h>
class stageObject
{
public:
GLfloat x, y;
//sprites
stageObject();
virtual void update()=0;
virtual void draw()=0;
private:
// add your private declarations
};
#endif // STAGEOBJECT_H
Code: Select all
#ifndef HECTOR_H
#define HECTOR_H
#include "stageobject.h"
class hector: public stageObject
{
public:
int test;
hector();
virtual ~hector();
void update();
void draw();
private:
// add your private declarations
};
#endif // HECTOR_H
Code: Select all
#ifndef STAGE_H
#define STAGE_H
#include <list>
#include "stageobject.h"
class stage
{
public:
std::list<stageObject*> stageObjects;
std::list<stageObject*>::iterator i;
stage();
private:
// add your private declarations
};
#endif // STAGE_H
Code: Select all
#include "stage.h"
#include "hector.h"
stage::stage()
{
hector *dude = new hector;
stageObjects.push_back(dude);
}
Now, in my main I make a new stage and try to iterate through the list and assign a value to one of the stageObject's member variables...
Code: Select all
stage *gameStage = new stage;
//try to interate thru that list that stage has
for(gameStage->i=gameStage->stageObjects.begin(); gameStage->i != gameStage->stageObjects.end(); ++gameStage->i)
{
gameStage->i->x = 44;
}
Code: Select all
main.cpp:196: error: request for member ‘x’ in ‘* gameStage->stage::i.std::_List_iterator<_Tp>::operator-> [with _Tp = stageObject*]()’, which is of non-class type ‘stageObject*’