Trouble using class method as sort predicate

Discussion in 'C Programming' started by scholtes, Aug 4, 2008.

  1. scholtes

    scholtes

    Joined:
    Aug 4, 2008
    Messages:
    1
    Location:
    Austin
    I'm trying to sort an STL vector. If I write:
    Code:
    [COLOR="Blue"]sort(vec.begin(), vec.end(), objLess);[/COLOR]
    where:
    Code:
    [COLOR="Blue"]bool objLess(const Foo& left, const Foo& right){
      bool result = fooCompare();
      return result;
    }[/COLOR]
    it works. But if I try to make the predicate objLess a class method:

    Code:
    [COLOR="blue"]bool MyClass::objLess(const Foo& left, const Foo& right) {...}[/COLOR]
    then it won't compile (under g++). The errors start off:

    error: no matching function for call to `sort(__gnu:cxx:__normal_iterator<Foo*,std::vector<Foo, std:allocator<Foo> > >, _gnu_cxx::__normal_iterator<Foo*, std::vector<Foo, std::allocator<Foo> > >, <unresolved overloaded function type>)'
    note: candidates are: void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx:__normal_iterator<Foo*, std::vector<Foo, std::allocator<Foo> > >, _Compare = bool (MyClass::*)(const Foo&, const Foo&)]


    Somone suggested name-mangling might be involved, but I can't figure this out. What's going on here?

    Regards,
    Mike
    scholtes, Aug 4, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hall
    Replies:
    4
    Views:
    656
  2. mscava@gmail.com

    std::sort std::vector predicate problem

    mscava@gmail.com, Apr 6, 2007, in forum: C++
    Replies:
    1
    Views:
    484
    Marcus Kwok
    Apr 6, 2007
  3. jason.cipriani@gmail.com

    Setting predicate for std::list::sort().

    jason.cipriani@gmail.com, Mar 18, 2008, in forum: C++
    Replies:
    4
    Views:
    472
    jason.cipriani@gmail.com
    Mar 18, 2008
  4. Ganesh
    Replies:
    3
    Views:
    353
    James Kanze
    Sep 30, 2008
  5. Victor Bazarov
    Replies:
    3
    Views:
    821
    James Kanze
    Feb 2, 2010
Loading...

Share This Page