Boost logo

Boost Users :

From: Leon Mergen (leon_at_[hidden])
Date: 2006-04-09 12:30:22


Hello,

Ok, this is what I'm trying to do: I want to define some base class A
which has a container with elements it needs to sort. I want to be
flexible with the comparison function, so the base class A will define
an abstract comparison function which will be implemented by derived
classes.

Since std::sort () expects function objects, after a lot of asking
around I found out I needed to use boost::bind. However, trying almost
all different types of combinations and looking up all kind of manuals,
I'm pretty much clueless...

Here's the test-case code:

---
#include <algorithm>
#include <string>
#include <vector>
#include <boost/bind.hpp>
class A {
public:
virtual bool sort (std::string const * const & a, std::string const * 
const & b) const = 0;
void func () {
// std::sort ( _elements.begin(), _elements.end(), boost::mem_fn ( 
A::sort) );
std::sort ( _elements.begin(), _elements.end(), boost::bind ( A::sort, 
this, _1, _2 ) );
}
private:
std::vector < std::string * > _elements;
};
class B : public A {
public:
virtual bool sort (std::string const * const & a, std::string const * 
const & b) const {
return true;
}
};
class C : public A {
public:
virtual bool sort (std::string const * const & a, std::string const * 
const & b) const {
return false;
}
};
---
I hope the code makes it a bit more clear as to what I'm trying to do... 
I follow examples listed at threads like 
http://blog.gmane.org/gmane.comp.lib.boost.user/day=20050617 , and well, 
this is almost a literate copy of some recommendations but it doesn't work:
--
sort.cpp: In member function ‘void A::func()’:
sort.cpp:13: error: no matching function for call to ‘bind(<unknown 
type>, A* const, boost::arg<1>&, boost::arg<2>&)’
/usr/local/include/boost/bind.hpp:1416: note: candidates are: 
boost::_bi::bind_t<boost::_bi::unspecified, F, typename 
boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(F, A1, A2, A3) 
[with F = bool (A::*)(const std::string* const&, const std::string* 
const&)const, A1 = A*, A2 = boost::arg<1>, A3 = boost::arg<2>]
/usr/local/include/boost/bind/bind_mf_cc.hpp:78: note: 
boost::_bi::bind_t<R, boost::_mfi::cmf2<R, T, A1, A2>, typename 
boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::*)(B1, 
B2)const, A1, A2, A3) [with R = bool, T = A, B1 = const std::string* 
const&, B2 = const std::string* const&, A1 = A*, A2 = boost::arg<1>, A3 
= boost::arg<2>]
--
and well, the error is cryptic enough for me to not quite understand 
what I'm doing wrong... any suggestions ?
Thanks in advance!
Regards,
Leon Mergen

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net