vector<>::iterator question [SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

vector<>::iterator question [SOLVED]

Post by captjack »

This is probably so simple that I can't see the obvious right in front of me. Nevertheless, I've thrown my hands in the air and pitch this to you guys:

Code: Select all

class MapObject {
public:
     int get_x();     // method implementation is obvious so i'll spare you the details
     int get_y();     // ditto
     ...
private:
     int x;
     int y;
     ...
};

Code: Select all

class Map {
public:
     void map_debug();
     ...
private:
     vector<MapObject*> mapObjects;
     ...
};

void Map::map_debug()
{
     ...
     for (vector<MapObject*>::iterator i = mapObjects.begin(); i != mapObjects.end(); i++)
          dbg.Print("(x, y) = (%d, %d)\n", i->get_x(), i->get_y());          // thanks gyrovorbis for debug singleton vid
}
Let's assume I didn't make a typo, and all necessary #include's are there, because the code compiles fine except for the vector<MapObject*>::iterator line.

Code: Select all

error C2839: invalid return type 'Object **' for overloaded 'operator ->'
error C2039: 'get_x' : is not a member of 'std::_Vector_iterator<_Ty,_Alloc>'
If I change Map::map_debug() to:

Code: Select all

void Map::map_debug()
{
     ...
     for (unsigned int i = 0; i < mapObjects.size(); i++)
          dbg.Print("(x, y) = (%d, %d)\n", mapObjects[i]->get_x(), mapObjects[i]->get_y());
}
then I get a successful compile and the intended output in the debug log, which is the (x, y) coordinates of map objects pushed into the vector container.

Why the hell can I not use the idiomatic vector<MapObject*>::iterator? I get the feeling that I'm not redirecting a pointer correctly, but I'm baked.

-capt jack
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Re: vector<>::iterator question [SOLVED]

Post by captjack »

Oh, Jesus!

I took all that time to post my problem with such pretty formatting ---- and then I figured out the problem!

Code: Select all

...
void Map::map_debug()
{
     for (vector<MapObject*>::iterator i = mapObjects.begin(); i != mapObjects.end(); i++)
          dbg.Print("(x, y) = (%d, %d)\n", (*i)->get_x(), (*i)->get_y());
}
When one sleeps on a problem overnight, one should try to work on said problem the next day before one goes to the trouble of posting a tome. :roll:

-capt jack
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: vector<>::iterator question [SOLVED]

Post by MarauderIIC »

When using non-standard types, ++i is a bit faster. I think the resources thing has a "how to use STL properly" link, and it includes stuff like that.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Re: vector<>::iterator question [SOLVED]

Post by captjack »

MarauderIIC wrote:When using non-standard types, ++i is a bit faster. I think the resources thing has a "how to use STL properly" link, and it includes stuff like that.
Oh, crap. That was a cut n' paste boo-boo, but thanks for spotting it and bringing it up. Your point is one that does bear repeating now an again.

BTW, thanks for the OO design reply. I got the idea for MapObjects from your code which was more approachable than what I had originally wrote.

-capt jack
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: vector<>::iterator question [SOLVED]

Post by avansc »

class Map : public vector<MapObject*>
{
}

if you wanna really OO it.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Re: vector<>::iterator question [SOLVED]

Post by captjack »

avansc wrote:class Map : public vector<MapObject*>
{
}

if you wanna really OO it.
Nice! A little more step-wise refinement never hurt a source file. It pays to remember the expanse of inheritance. It also pays to not do this when you're tired. :lol:

Ever get so caught up in coding that you forget to use the fundamentals?

-capt jack
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: vector<>::iterator question [SOLVED]

Post by MarauderIIC »

Neat, avansc.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply