首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用boost::function和boost::bind

使用boost::function和boost::bind
EN

Stack Overflow用户
提问于 2015-12-10 19:34:50
回答 1查看 910关注 0票数 3

下面的代码试图将非静态成员函数作为替代思想传递给旧的c代码,在这里它需要函数指针。这不是在汇编。你能帮忙吗?有些事情肯定是显而易见的,我对此并不熟悉。提前谢谢。-金平

代码语言:javascript
复制
#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_-O0/实用程序_sys/boost_1_42_0/-i/usr/g++_i_meth_-O0/实用程序_sys/gsl-1.15/ -DUNIX -DSET_ENVIRONMENT -DOPTION_RESET -c ././src/BindTest.cpp/meth_g++_-O0/效用_sys/boost_1_42_0/boost/ function /function_template.hpp:在静态成员函数中-˜静态R˜( /meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:913:) T0 ( FunctionObj = boost::_bi::bind_t,boost::_bi::list1 > >,R= double,T0=double欧元™:boost::_bi::bind_t˜void boost::function1::assign_to(函子)实例化而函子= boost::_bi::bind_t,boost::_bi::list1 > >,R= double,函数1::/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:722: 1(函子,类型名称boost::enable_if_c::value>::value,int>::type),函子= boost::_bi::bind_t,boost::_bi::list1 > >,R= double,函数::/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:1064:(函子,类型名称boost::enable_if_c::value>::value,int>::type),函子= boost::_bi::bind_t,boost::_bi::list1 > >,R= double,/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132: T0 =double欧元™./src/bindTest.cpp:46:从这里实例化/meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:˜double()(Double)™.(Double)™./src/bindTest.cpp:46:从这里实例化:无法将™˜double()(Double)™™转换为-欧元˜double欧元™,以换取成员函数-欧元˜R& boost::_mfi::dm::operator中的T0()(T) 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实例化为R= double (&)(double),F= boost::_mfi::dm,A= boost::_bi::list1,/meth_mount/utility_sys/boost_1_42_0/boost/bind/bind_template.hpp:32: A1 = boost::_bi::value’boost::_bi::result_traits::type boost::_bi::bind_t::operator()(A&),A1 = double,R= double (&)(double),F= boost::_mfi::dm,用/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:132:= boost::_bi::bind_t,boost::_bi::list1 > >,从FunctionObj =boost::_bi::bind_t,boost::_bi::list1 >,R= double,从FunctionObj˜静态R T0实例化的L=FunctionObj>-欧元™/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:913:=double欧元™boost::function1::assign_to(函子),函子= boost::_bi::bind_t,boost::_bi::list1 > >,R= double,T0 =double欧元™boost::function1::assign_to实例化自-欧元˜boost::Functor 1::Functor 1(函子,类型名称/meth_mount/utility_sys/boost_1_42_0/boost/function/function_template.hpp:1064: boost::enable_if_c::value>::value,int>::type),其中函子= boost::_bi::bind_t,boost::_bi::list1 > >,R= double,T0 =double欧元™boost::enable_if_c::value>::value实例化自-欧元˜boost::function::function( Functor,typename boost::enable_if_c::value>::value,)与函子= /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp:342:,boost::_bi::list1 >,R= double,T0 =double欧元™././src/bindTest.cpp:46:从这里实例化的boost::_bi::bind_t错误:无效使用非静态成员函数make:*./bin/BindTest.o错误1

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-10 19:39:25

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

代码语言:javascript
复制
double res2 = addTest(boost::bind(&X::plusIt, &newx, _1), 10, 5);

另见使用绑定和指向成员的指针

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34210264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档