使用boost :: function和boost :: bind

Jinping Shi

我有以下代码尝试将非静态成员函数作为替换想法传递给需要功能指针的旧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 ++-墙-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)实例化[带有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,类型名称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,类型名boost :: enable_if_c :: value> :: value,int> :: type)实例化[带有Functor = boost :: __ bi :: bind_t,boost :: _ bi :: list1>>,R =两倍,T0 =双重]’./../src/BindTest.cpp:46:从此处实例化/ meth_mount / utility_sys / boost_1_42_0 / boost / function / function_template.hpp:132:错误:无法转换– double(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,类型名称boost :: enable_if_c :: value> :: value,int> :: type)实例化[与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:错误:无法转换成双倍(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,类型名称boost :: enable_if_c :: value> :: value,int> :: type)实例化[与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:错误:无法转换成双倍(类型名称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:错误:无法转换–类型名称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''作为回报​​/meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:在成员函数``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:从“ 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:从“类型名称” 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:从“静态R boost :: detail :: function :: function_obj_invoker1 :: invoke(boost :: detail :: function)实例化:: function_buffer&,T0)[with FunctionObj = boost :: _ bi :: bind_t,boost :: __ bi :: list1>>,R =两倍,T0 =双重]’/ meth_mount / utility_sys / boost_1_42_0 / boost / function / function_template.hpp:913:从“ void boost :: function1 :: assign_to(Functor)实例化[带有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,类型名称boost :: enable_if_c :: value> :: value,int> :: type)[with Functor = boost :: _ bi :: bind_t,boost :: _ bi :: list1> >,R =两倍,T0 =双重]’/ meth_mount / utility_sys / boost_1_42_0 / boost / function / function_template.hpp:1064:从–boost :: function :: function(Functor,类型名称boost :: enable_if_c实例化) :: value> :: value,int> :: type)[使用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

维他命

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

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

另请参见使用带有指向成员的指针的绑定

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

澄清使用`boost :: bind`和`this`

std :: bind和boost :: bind技巧

使用Boost lambda和bind排序时出错

ROS 订阅回调 - 使用 boost::bind 和成员函数

如何使用boost :: bind将静态成员函数绑定到boost :: function

使用Boost.Bind打印矢量元素

使用 boost::bind 结果作为参数

使用boost :: bind传递成员函数

从boost :: bind_t自动转换为boost :: function

Boost Asio - boost::bind 导致程序崩溃

使用find_if和boost :: bind与一组shared_pointers

如何使用boost :: bind正确绑定成员函数

使用boost :: bind进行订阅回调时出错

我必须使用boost :: bind创建多态的“转换”功能吗?

当我添加头文件“ boost / function”时,使用“ bind”对重载函数进行歧义调用

将boost功能与带有地图的boost bind结合使用

ASIO处理程序参数和boost :: bind,编译时错误

Boost.Python和Boost.Function

编译Boost.Bind时出错

在我的函数中不再需要boost :: bind

boost :: bind为void指针参数

boost::bind not saving partial arguments when stored

如何将boost ::: bind绑定到boost :: math :: pdf?

没有使用boost :: bind的调用错误匹配功能

使用boost :: bind()在std :: min_element()的自定义函数中填充结构

Boost.Asio:不能使用std :: bind()指定回调

使用boost :: future和continuation和boost :: when_all

“ C ++中没有“引用成员”类型”,std :: bind和boost :: bind在同一条船上

使用boost :: visitor和unique_ptr的boost :: variant