• Advertisement
Sign in to follow this  

vectore problems again

This topic is 3652 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

Why does this not work when the 2nd one does?
	for (std::vector<object*>::iterator it = object_list_depth.end(); it != object_list_depth.begin(); it--)
		if((*it)!=NULL && (*it)->active) (*it)->draw();//Vector iterator is not dereferencable

		for (std::vector<object*>::iterator it = object_list_depth.begin(); it != object_list_depth.end(); it++)
			if((*it)!=NULL && (*it)->active) (*it)->draw();

Share this post


Link to post
Share on other sites
Advertisement
stl vector's end method returns 1 pass the end of the list, so the first deference isn't actually returning anything in the list, try using:


for (std::vector<object*>::iterator it = object_list_depth.end()-1; it != object_list_depth.begin(); it--)
if((*it)!=NULL && (*it)->active) (*it)->draw();//

Share this post


Link to post
Share on other sites

...vc\include\vector
Line: 163

Expression:("_Myptr + _Off <= ((_Myvec *)(this->_Getmycount()))->_Mylast&&_Myptr+_off >=((_Myvec *)(this->Getmycount()))->_Myfirst",0)


for (std::vector<object*>::iterator it = object_list_depth.end()-1; it != object_list_depth.begin(); it--)//HERE
if((*it)!=NULL && (*it)->active) (*it)->draw();

Share this post


Link to post
Share on other sites
Don't.

The operation v.end() - 1 will obviously fail on an empty vector. Instead of making life hard for yourself using normal iterators, use std::vector::rbegin and std::vector::rend.

Share this post


Link to post
Share on other sites
you could also try using a reverse_iterator ... so:(check that syntax though I don't have an IDE in front of me)

for (std::vector<object*>::reverse_iterator it = object_list_depth.begin(); it != object_list_depth.end(); it++)
if((*it)!=NULL && (*it)->active) (*it)->draw();

Share this post


Link to post
Share on other sites
for (std::vector<object*>::reverse_iterator it = object_list_depth.rbegin(); it != object_list_depth.rend(); it++)
if((*it)!=NULL && (*it)->active) (*it)->draw();

EDIT: note just fixed RIZAXs example, he forgot to add those 'r's.

[Edited by - MaR_dev on February 22, 2008 6:28:40 AM]

Share this post


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

  • Advertisement