2015-12-10 4 views
3

У меня есть следующий код, пытающийся передать нестатические функции-члены как заменяющую идею на старый код c, где требуется указатель на функцию. Он не компилируется. Не могли бы вы помочь? Что-то должно быть очевидно, и я новичок в этом. Заранее спасибо. - ЦзиньпинИспользование boost :: function и boost :: bind

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

// Ошибка компиляции:

г ++ -Wall -g -O0 -I/meth_mount/utility_sys/boost_1_42_0/-I/USR/включать -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: В статической функции-члене â € ~static R boost :: detail :: function :: function_obj_invoker1 :: invoke (boost :: detail :: function :: function_buffer &, T0) [с 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, typename boost :: enable_if_c :: значение> :: значение, int> :: type) [с помощью 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 :: значение> :: значение, 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: ошибка: невозможно преобразовать â € ~double () (двойной) â € ™ to â € ~doubleâ € ™ взамен /meth_mount/utility_sys/boost_1_42_0/boost/bind/mem_fn.hpp: В функции-члене â € ~R & boost :: _ mfi :: dm :: operator() (T) const [с 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, A1 = boost :: _ bi :: значение] â â â â â ty ty ty ty ty ty pename boost :: _ bi :: result_traits :: type boost :: _ bi :: bind_t :: operator() (A1 &) [с 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) [с 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: insta ntiated from â € ~boost :: function1 :: function1 (Functor, typename boost :: enable_if_c :: значение> :: значение, int> :: type) [с помощью 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: экземпляр из функции «функция: функция :: функция» (Functor, typename boost :: enable_if_c :: значение> :: значение, 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: ошибка: неправильное использование нестатической функции члена марки: *** [../bin/BindTest.o] Ошибка 1

+0

Boost 1.42? В самом деле? – SergeyA

ответ

5

У вас не хватает _1, который необходим, потому что X::plusIt является одноместный функцией (не считая this). Правильный код

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

Смотрите также Using bind with pointers to members.

+1

Вот оно, спасибо! Такой подход настолько опрятен. Есть ли подобные вещи в java? –

+0

Посмотрите на http://stackoverflow.com/questions/15053127/equivalent-of-cs-stdbind-in-java – vitaut

Смежные вопросы