下面的代码试图将非静态成员函数作为替代思想传递给旧的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_-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
发布于 2015-12-10 19:39:25
您缺少了_1,这是必需的,因为X::plusIt是一个一元函数(不包括this)。正确的代码是
double res2 = addTest(boost::bind(&X::plusIt, &newx, _1), 10, 5);另见使用绑定和指向成员的指针。
https://stackoverflow.com/questions/34210264
复制相似问题