Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Trouble using class method as sort predicate

Reply
Thread Tools

Trouble using class method as sort predicate

 
 
scholtes scholtes is offline
Junior Member
Join Date: Aug 2008
Location: Austin
Posts: 1
 
      08-04-2008
I'm trying to sort an STL vector. If I write:
Code:
sort(vec.begin(), vec.end(), objLess);
where:
Code:
bool objLess(const Foo& left, const Foo& right){
  bool result = fooCompare();
  return result;
}
it works. But if I try to make the predicate objLess a class method:

Code:
bool MyClass::objLess(const Foo& left, const Foo& right) {...}
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting observation (predicate with std::sort) Victor Bazarov C++ 3 02-02-2010 10:24 PM
std:sort predicate function optional arguments Ganesh C++ 3 09-30-2008 09:16 AM
Setting predicate for std::list::sort(). jason.cipriani@gmail.com C++ 4 03-18-2008 11:34 PM
std::sort std::vector predicate problem mscava@gmail.com C++ 1 04-06-2007 05:16 PM
trouble when overloading function used as predicate in std::sort hall C++ 4 07-03-2004 12:13 PM



Advertisments