I have the following code trying to pass non-static member functions as a replacement idea to old c code where it requires function pointer. It is not compiling. Could you help? Something must be obvious and I am new to this. Thanks in advance. -- Jinping
#include <iostream>
#include <boost/function.hpp>
#include <boost/function_equal.hpp>
#include <boost/bind.hpp>
using namespace boost;
using namespace std;
double addTest( boost::function<double (double)> f, double a, double x )
{
double a1 = f(a);
double a2= a1+x;
return a2;
}
double squareIt (double x) {
return x*x;
}
class X {
public:
X(double x0, double x1){ x=x0+x1; }
double plusIt(double t) { return x+t; }
private:
double x;
};
int main (int argc, char** argv)
{
boost::function<double (double)> f;
f = &squareIt;
double result = addTest(f, 10, 5); //OK
cout << "result = " << result << endl;
X newx(10, 15);
//f=boost::bind(&X::plusIt, &newx); // does not complie
double res2 = addTest(boost::bind(&X::plusIt, &newx), 10, 5); // does not compile
cout << "result2 = " << res2 << endl;
return 0;
}
// compile error:
g++ -Wall -g -O0 -I/meth_mount/utility_sys/boost_1_42_0/ -I/usr/include -I/meth_mount/utility_sys/gsl-1.15/ -I/home/ayuan/work/ird_lib/core_lib/alib/intrface/build/alib/clib/linux -DUNIX -DSET_ENVIRONMENT -DOPTION_RESET -c ./../src/BindTest.cpp /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp: In static member function ‘static R boost::detail::function::function_obj_invoker1::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’: /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:913: instantiated from ‘void boost::function1::assign_to(Functor) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:722: instantiated from ‘boost::function1::function1(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:1064: instantiated from ‘boost::function::function(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ ./../src/BindTest.cpp:46: instantiated from here /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132: error: cannot convert ‘double ()(double)’ to ‘double’ in return /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp: In member function ‘R& boost::_mfi::dm::operator()(T) const [with R = double ()(double), T = X]’: /meth_mount/utility_sys/boost_1_42_0/boost/bind/bind.hpp:243: instantiated from ‘R boost::_bi::list1::operator()(boost::_bi::type, F&, A&, long int) [with R = double (&)(double), F = boost::_mfi::dm, A = boost::_bi::list1, A1 = boost::_bi::value]’ /meth_mount/utility_sys/boost_1_42_0/boost/bind/bind_template.hpp:32: instantiated from ‘typename boost::_bi::result_traits::type boost::_bi::bind_t::operator()(A1&) [with A1 = double, R = double (&)(double), F = boost::_mfi::dm, L = boost::_bi::list1 >]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132: instantiated from ‘static R boost::detail::function::function_obj_invoker1::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:913: instantiated from ‘void boost::function1::assign_to(Functor) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:722: instantiated from ‘boost::function1::function1(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:1064: instantiated from ‘boost::function::function(Functor, typename boost::enable_if_c::value>::value, int>::type) [with Functor = boost::_bi::bind_t, boost::_bi::list1 > >, R = double, T0 = double]’ ./../src/BindTest.cpp:46: instantiated from here /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:342: error: invalid use of non-static member function make: *** [../bin/BindTest.o] Error 1