I'm having trouble with using a multiset of my own data structure. It seems to complain about my comparison (less than) function when I try to iterate, but it works at some level because I can insert() stuff and call functions from the first member of the multiset. If anyone can point me in the right direction of solving my problem I'd appreciate it.

struct sprites {
unsigned int x_coord;
unsigned int y_coord;
short int animation;

sprites(unsigned int, unsigned int, short int);
void draw(void) const;
bool operator< (const sprites& cmpParam) const;
};

bool sprites::operator< (const sprites& cmpParam) const {
return(x_coord < cmpParam.x_coord);
}

in the main code:
for(multiset<sprites>::iterator iter=fg_sprites.begin(); iter<fg_sprites.end(); iter++) iter->draw(); //does not work
fg_sprites.begin()->draw(); // works

You should write iter!=fg_sprites.end() rather than iter<fg_sprites.end() because the multiset type offers a bidirectional iterator, not a random-access iterator, and only random-access iterators support <.

(also, it is preferable to write ++iter rather than iter++ because there's no need to copy the value of iter and then throw the copy away without doing anything with it)

thanks a lot. it compiles and works like a charm and I learned something to boot. :o

edit: guess the compiler errors didn't refer to the templating of my custom < operator, but rather the (nonexistant) < operator of the iterator.

Edited 6 Years Ago by Twinzen: n/a

This article has been dead for over six months. Start a new discussion instead.