Page 1 of 2

Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:12 pm
by JS Lemming
So I have a stageObject class...

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
And just fyi, a hector class with inherits from it...

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
Now, I have a stage class that contains a list and list interator for stageObjects...

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
When that class is constructed it makes a hector and puts it in the list...

Code: Select all

#include "stage.h"
#include "hector.h"

stage::stage()
{
	hector *dude = new hector;
	stageObjects.push_back(dude);
}
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...

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;
		}
It iterates through the list just fine. However, the act of trying to access that x member gives me this error:

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*’
I have a feeling it's something simple I'm missing... Any help would be appreciated.

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:22 pm
by Falco Girgis
Hrm, I haven't really messed with std::iterator and std::list before, but it looks like an inheritance issue to me. Looks like the Hector object isn't polymorphing back into a stage object when you're referencing it. Have you tried to explicitly typecast it like this?

Code: Select all

(stageObject *)gameStage->i->x = 44;

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:24 pm
by JS Lemming
Yes, it yields an identical error.

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:36 pm
by Falco Girgis
I've been staring at this for like 20 minutes, I'm starting to get pissed. I don't see anything wrong. I even learned C++ lists and iterators just to look at this more.

This won't be a neat or pretty fix, but I'm curious if you can do this:

Code: Select all

stageObject *temp = gameStage->i;
temp->x = 44;
Oh, and my typecast above was both pointless and stupid. Typecasting what, a GLFloat to a stageObject pointer? XD

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:42 pm
by M_D_K
JS Lemming wrote:So I have a stageObject class...

Code: Select all

#include "stage.h"
#include "hector.h"

stage::stage()
{
	hector *dude = new hector;
	stageObjects.push_back(dude);
}
EDIT: ^^ btw, does anyone know of a nicer way of doing that so it's all on one line... and not needing of dude?

Code: Select all

stageObjects.push_back(new hector);//this should work ;)
to access the iterator do this

Code: Select all

(*i)->SomeFunction();

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:47 pm
by Falco Girgis
M_D_K wrote:

Code: Select all

(*i)->SomeFunction();
Oh my god, you have to be kidding me.

edit: I see what you did there. 0_o

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 4:48 pm
by M_D_K
GyroVorbis wrote:
M_D_K wrote:

Code: Select all

(*i)->SomeFunction();
Oh my god, you have to be kidding me.
:lol:

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:19 pm
by JS Lemming
M_D_K wrote:to access the iterator do this

Code: Select all

(*i)->SomeFunction();
I tried that earlier and got some new error.

I did...

Code: Select all

gameStage->(*i)->x = 44;
and got this...

Code: Select all

main.cpp:196: error: expected unqualified-id before ‘(’ token
main.cpp:196: error: ‘i’ was not declared in this scope
Or am I missing what you meant?
Oh, and my typecast above was both pointless and stupid. Typecasting what, a GLFloat to a stageObject pointer? XD
haha I did it too so we're both retards.

EDIT: oops, I missed your idea Gyro. I'll try that in a min.

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:23 pm
by M_D_K
try removing the "(" and ")"(i'm to lazy to try and spell them)

so:

Code: Select all

gameStage->*i->x = 44;

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:24 pm
by JS Lemming
Removing the () just results in the second error: ‘i’ was not declared in this scope

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:28 pm
by M_D_K
well Why don't you just go

Code: Select all

      for(uint i  = 0;i < gameStage->stageObjects.size(); ++i)
      {
         gameStage->stageObjects[i]->x = 44;
      }

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:34 pm
by Falco Girgis
Okay, serious question.

What the fuck does an iterator return? An integer? The indirection operator is overloaded to return a pointer to the current object?

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 7:39 pm
by M_D_K
An iterator acts as an index for the list. When dereferenced it acts as the object at the point of the list its pointing to.

I think...

Re: Difficulty accessing a member of a class from a list

Posted: Wed Dec 24, 2008 9:29 pm
by JS Lemming
M_D_K wrote:well Why don't you just go

Code: Select all

      for(uint i  = 0;i < gameStage->stageObjects.size(); ++i)
      {
         gameStage->stageObjects[i]->x = 44;
      }
An array? That's soo..... I dunno.... I've always had trouble with these dang lists in the past and I'm determined to finally get them working.


EDIT! Falco you are my hero! I tried a slight alteration of your idea and got it working.

I had to do...

Code: Select all

			stageObject *temp = *gameStage->i;
			temp->x = 44;
Now all I need to know is why the hell do I have to do that. It's essentially the same as in above posts but expanded out. Makes me think the syntax for what I was trying is more complicated than what I tried.


Moe Edit: Aight. Figured it out. It was just tricky parenthesis placement. Thanks guys.

Code: Select all

(*gameStage->i)->x =44;

Re: Difficulty accessing a member of a class from a list

Posted: Thu Dec 25, 2008 2:09 pm
by M_D_K
JS Lemming wrote: An array? That's soo..... I dunno.... I've always had trouble with these dang lists in the past and I'm determined to finally get them working.
Not really you'd still be using an std::list just accessing it like an array.
JS Lemming wrote: Moe Edit: Aight. Figured it out. It was just tricky parenthesis placement. Thanks guys.

Code: Select all

(*gameStage->i)->x =44;
I can't believe I missed that...my leetness is fading :(