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

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.