Jump to content
  • Advertisement
Sign in to follow this  
Meganan

Vector Problems (Iterators) - no more now SOLVED

This topic is 4505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!