Difficulty accessing a member of a class from a list

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Difficulty accessing a member of a class from a list

Post 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.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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;
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post by JS Lemming »

Yes, it yields an identical error.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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();
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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:
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post 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.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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;
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post by JS Lemming »

Removing the () just results in the second error: ‘i’ was not declared in this scope
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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;
      }
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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?
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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...
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post 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;
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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 :(
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply