Vector Problems (Iterators) - no more now SOLVED

Started by
5 comments, last by Meganan 17 years, 12 months ago
I am using Visual C++ 6.0 and the problem I am having is probably very simple. I have this code: Declaration of some used variables: vector<object*> m_objects; vector<object*>::iterator iter; The place where it all goes wrong:

void RenderObjects()
{
	for(iter = m_objects.begin(); iter = m_objects.end(); ++iter)
	{
		iter->Render();
	}
}






Now the error that is given is: error C2227: left of '->Render' must point to class/struct/union I don't understand what i'm doing wrong. The compiler can see all the variables but it is asthough it doesn't recognise iter as an object. [Edited by - Meganan on April 20, 2006 3:16:14 AM]
<-> Rate someone badly if they say something that has absolutely nothing to do with your problem; don't rate people badly because they tried to help you with their limited knowledge. <->
Advertisement
(*iter)->Render();
Right you are...*sheepishly lowers head* (I feel so ashamed)

Thankyou jyk

Now I have another problem and i think it is best described by the code:
Where problems occur:main code...case VK_LEFT:	obj = new Swarmer;	g_glRender->AddNewObject(obj);	break;...Declaration of the different objects:class object{public:	object(float mx, float my, float mz): x(mx), y(my), z(mz){}	float x, y, z;	bool solid;	bool visible;	bool Step();	virtual void Render();};class Swarmer : public object{public:	bool Step();	virtual void Render();};The adding a new object functuon definition:bool AddNewObject(object* obj){	m_objects.push_back(obj);	return true;}


I am unable to discover what is going wrong and i get a linking error as follows:

error C2512: 'Swarmer' : no appropriate default constructor available

Thankyou for your time.

BTW object *obj = NULL; is declared at the start

[Edited by - Meganan on April 19, 2006 12:54:57 AM]
<-> Rate someone badly if they say something that has absolutely nothing to do with your problem; don't rate people badly because they tried to help you with their limited knowledge. <->
As the error says, there is no default (i.e. no parameters or all default parameters) constructor for swarmer.

You could add a swarmer::swarmer() constructor but then it will complain about no default constructor for object. You either need to also create a () constructor for object (that for example sets the x, y and z to zero) or

swarmer::swarmer(float mx,float my,float mz) : object(mx,my,mz) { }

and pass values in when you create the new swarmer

or perhaps

swarmer::swarmer() : object(0,0,0) { }

would create a swarmer with objects x,y and z being initially set to zero when you go new swarmer()

Most flexible would be

swarmer::swarmer(float mx=0,float my=0,float mz=0) : object(mx,my,mz) { }

then you could do either.
I'm just a newbie to C++, but isn't there a problem with that loop?

for(iter = m_objects.begin(); iter = m_objects.end(); ++iter)

Shouldn't it be "iter != m_objects.end()"? The way it is written isn't iter just being set to end() the first time through the loop?

eugenek
Quite correct, AP, as far as I can see. Either != or < would be right. Can't remember which.
Yes yes you were right I noticed that myself.

BTW i spend a good ten - fifteen hours on this and i finally got it working. I'm pretty estatic about it. I'm finally getting there; now theres only the rest of my life worth of learning ahead. LOL.

Thankyou all for your help.

N.B. I think i can safely say this is solved...so i'll edit the topic...done
<-> Rate someone badly if they say something that has absolutely nothing to do with your problem; don't rate people badly because they tried to help you with their limited knowledge. <->

This topic is closed to new replies.

Advertisement