Jump to content
  • Advertisement
Sign in to follow this  

stl map and interators

This topic is 4718 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 have the following map map<string, list<SceneObject*>* > myMap; Now I'm trying to create an iterator to go through all the elements in this map. I've tried doing the following but the the error: [C++ Error] _alloc.h(416): E2406 Dependent type qualifier 'list<SceneObject *,allocator<SceneObject *> > *' is not a class or struct type list<string, list<SceneObject*>*>::iterator itr; Does anyone know how to do this correctly ?

Share this post


Link to post
Share on other sites
Advertisement
The only thing that jumps out is that you're using a list iterator for a map container.

Change: list<string, list<SceneObject*>*>::iterator itr;
To: map<string, list<SceneObject *> *>::iterator itr;

Share this post


Link to post
Share on other sites
duh! didn't see him put 'list' instead of map :P

int main(int argc , char **argv)
{
map<string , list<int*>*> x;
map<string , list<int*>*>::iterator it;

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by JY
The only thing that jumps out is that you're using a list iterator for a map container.

Change: list<string, list<SceneObject*>*>::iterator itr;
To: map<string, list<SceneObject *> *>::iterator itr;

Or, to avoid this kind of mistakes in the future (and to allow for faster switching between container types:

typedef std::list<SceneObject*> object_list;
typedef std::map<std::string, object_list*> object_map;

// and then:

object_list* objects = new object_list;
SceneObject* o1 = new SceneObject;
SceneObject* o2 = new SceneObject;
SceneObject* o3 = new SceneObject;

objects->push_back( o1 );
objects->push_back( o2 );
objects->push_back( o3 );

object_map myMap;
myMap.insert( object_map::value_type( "foo", objects ) );

// and then:

// error check for find() should be included
// 'second' doesn't have to point to anything valid
object_list* list = myMap.find("foo")->second;
object_list::iterator it, end;
for( it = list->begin(), end = list->end();
it != end;
++it ) {

(*it)->bar();
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!