Trouble using class method as sort predicate

Joined
Aug 4, 2008
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top