Page 1 of 1

vector<>::iterator question [SOLVED]

Posted: Fri Oct 09, 2009 9:06 am
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

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

Posted: Fri Oct 09, 2009 9:14 am
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

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

Posted: Fri Oct 09, 2009 10:36 am
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.

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

Posted: Fri Oct 09, 2009 12:07 pm
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

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

Posted: Fri Oct 09, 2009 12:25 pm
by avansc
class Map : public vector<MapObject*>
{
}

if you wanna really OO it.

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

Posted: Fri Oct 09, 2009 1:50 pm
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

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

Posted: Fri Oct 09, 2009 5:35 pm
by MarauderIIC
Neat, avansc.