Jump to content
  • Advertisement
Sign in to follow this  
crucifer

little trouble using the vector STL

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

Hi, I am currently having a little bug with the vector STL object. When I push back pointers to classes, and use an iterator to access thoses classes, the visual studio intellisense allows me to do it and even enumerates the public variable/functions for me. But when I compile, I get the fallowing error :
Quote:
error C2440: 'type cast' : cannot convert from 'std::vector<_Ty,_Ax>::iterator' to 'SomeStructure *' with [ _Ty=SomeStructure *, _Ax=std::allocator<SomeStructure *> ]
Here is the source code
#include <iostream.h>
#include <vector>

using namespace std;

struct SomeStructure {

	int var;

};

void main() {

	vector<SomeStructure*> someVector;

	SomeStructure *someStruct = new SomeStructure;
	someStruct->var = 1;
	someVector.push_back(someStruct);

	vector<SomeStructure*>::iterator it;

	for(it=someVector.begin(); it!=someVector.end(); it++)
		cout << ((SomeStructure*) it)->var << endl;      //<---- here is the error
}

Can anyone find something wrong ?

Share this post


Link to post
Share on other sites
Advertisement
Ok, Iterators can be very cool, but when you have a list of elements in a vector, I prefer just going thru them one by one doing the following:

void main() {

vector<SomeStructure*> someVector;

SomeStructure *someStruct = new SomeStructure;
someStruct->var = 1;
someVector.push_back(someStruct);

for(int i=0; i<someVector.size(); i++)
cout << someVector->var << endl;

// Delete pointers
for (int i=0; i<someVector.size(); i++) {

delete someVector;
}

// Delete elements (this will be done in vector destructor,
// but it's not bad to be safe).
someVector.erase()
}



And it's hard to do anything wrong...

Btw, when you are storing pointers, you should remember to delete them last in your program, because the vector doesn't know you're storing pointers, they think it's just another data...

Albert

Share this post


Link to post
Share on other sites
The short answer is intellisense is working too hard.

The long answer is iterator::operator-> returns a pointer to the element. That is, in the case where the element is itself a pointer, an element**. The solution is to dereference the operator, then apply ->. (*iterator)->. I've seen it done as iterator->->, but I don't think that's standard. :D

Share this post


Link to post
Share on other sites
Quote:

Try:

cout << (*it)->var << endl;



Thx jyk ... it works fine ... I though I already tried that ... but it seems I was wrong.

Karma+ for you :-)

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!