【问题标题】:Using boost::function and boost::bind使用 boost::function 和 boost::bind
【发布时间】:2016-03-16 13:59:14
【问题描述】:

我有以下代码试图将非静态成员函数作为替代想法传递给需要函数指针的旧 c 代码。它没有编译。你能帮忙吗?有些东西一定很明显,我对此很陌生。提前致谢。 ——金平

#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;
}

//编译错误:

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:在静态成员函数中静态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: 实例化自 ‘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: 实例化自 `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: 实例化自 `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:从这里实例化 /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132:错误:无法将“double ()(double)”转换为“double”作为回报 /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:在成员函数中 'R& boost::_mfi::dm::operator()(T) const [with R = double ()(双),T = X]’: /meth_mount/utility_sys/boost_1_42_0/boost/bind/bind.hpp:243: 实例化自'R boost::_bi::list1::operator()(boost::_bi::type, F&, A&, long int ) [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: 实例化自'typename boost::_bi::result_traits::type boost::_bi::bind_t::operator()(A1&) [with A1 = 双,R = 双 (&)(双),F = boost::_mfi::dm,L = boost::_bi::list1 >]’ /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132: 实例化自'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: 实例化自 ‘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: 实例化自 `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: 实例化自 `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:从这里实例化 /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:342:错误:非静态成员函数的使用无效 make: *** [../bin/BindTest.o] 错误 1

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    您缺少_1,这是必需的,因为X::plusIt 是一元函数(不包括this)。正确的代码是

    double res2 = addTest(boost::bind(&X::plusIt, &newx, _1), 10, 5);
    

    另见Using bind with pointers to members

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多