Jump to content
  • Advertisement
Sign in to follow this  
fireside

operator overload sort problem

This topic is 4740 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'm trying to sort some pointers in a vector like this: theres a vector of pointers to objects called drawObjects
(overload in object header) 
 bool operator < (Obj * ob2){return getY() < ob2->getY();}

(in implementation);
for(int i=0;i<drawObjects.size();i++)
    std::cout<<drawObjects->getY()<<" ";
std::cout<<std::endl;
std::sort(drawObjects.begin(),drawObjects.end());
for(int i=0;i<drawObjects.size();i++)
    std::cout<<drawObjects->getY()<<" ";

output=
 700 500 500
700 500 500
How come it doesn't sort? I've also tried: bool operator < (Obj &ob2){return getY() < ob2.getY();} [Edited by - fireside on June 1, 2005 12:44:48 AM]

Share this post


Link to post
Share on other sites
Advertisement
operator< is not called because it's comparing pointers.

Try this:

bool less(const Obj* lhs, const Obj* rhs)
{
return lhs->getY() < rhs->getY();
}
...
std::sort(drawObjects.begin(), drawObjects.end(), less);

Regards
Mats

Share this post


Link to post
Share on other sites
I get an error:
world.cpp: In member function `void World::initLevel()':
world.cpp:101: error: missing template arguments before ')' token

which is on the line where the sort is called.

Hmm. I tried putting the less function in the header where sort was called and got this error:

g++ `pkg-config --cflags clanCore-0.7 clanDisplay-0.7 clanApp-0.7 clanGL-0.7 clanSound-0.7` -c -o world.o world.cpp
world.cpp: In member function `void World::initLevel()':
world.cpp:101: error: no matching function for call to `sort(__gnu_cxx::__normal_iterator<Obj**, std::vector<Obj*, std::allocator<Obj*> > >, __gnu_cxx::__normal_iterator<Obj**, std::vector<Obj*, std::allocator<Obj*> > >, <unknown type>)'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:2576: note: candidates are: void std::sort(_RandomAccessIterat

I think I found a page that explains a way:
http://www.devx.com/tips/Tip/20013


hope it works, haven't tried yet.

[Edited by - fireside on June 1, 2005 7:04:36 AM]

Share this post


Link to post
Share on other sites
Your way works the best Matsen, at least in a small test program. I'll have to figure out why it doesn't work in my main program. Thanks

Share this post


Link to post
Share on other sites
I believe the original problem may be with the type of operator< you used. I think you want the one that passes a reference instead of a pointer, as in "bool operator< (Obj& obj)". Did you try that?

Share this post


Link to post
Share on other sites
Yeah, I tried it. Didn't do anything because of the pointers, I guess. It's really frustrating because I can get it to work in a small program with the compare function, but in mine that's using the exact same code, I get some kind of unknown type during the implementation. I'm thinking of just writing a sort myself.

Share this post


Link to post
Share on other sites
The problem with the code in your original post is that you have overloaded operator < for Obj, but the vector contains Obj * and that's the operator std::sort is using.

The operator you would have to overload is for Obj *, not Obj. However, this have another problem; an overloaded operator must contain at least one user defined type, and pointers are not user defined types, so you can't overload the proper operator < to sort pointer to objects where the pointed to object is the sort key. That is why you have to provide a separate function (or function object) instead of using operator <.

operator < for pointers is defined to compare adresses. That means, your vector is being sorted, but by adress and not by your own sort criteria. So the pointer with smallest adress will appear first after sorting.

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!