2013-11-15 2 views
1

Я пытаюсь использовать повышение :: функции с методами экземпляра, используя следующий примериспользуя подталкивание :: функция с методами экземпляра

class someclass 
{ 
public: 

    int DoIt(float f, std::string s1) 
    { 
     return 0; 
    } 

    int test(boost::function<int(float, std::string)> funct) 
    { 
     //Funct should be pointing to DoIt method here 
     funct(12,"SomeStringToPass"); 
    } 

    void caller() 
    { 
       test(DoIt); //Error : 'someclass::DoIt': function call missing argument list; use '&someclass::DoIt' to create a pointer to member 
    } 
}; 

Любое предложение о том, как я могу решить эту проблему?

+0

Используйте 'зЬй :: function' и' зЬй :: bind' если вы ... – David

ответ

0

Вы должны использовать boost::bind:

#include <boost/function.hpp> 
#include <boost/bind.hpp> 
#include <string> 
#include <iostream> 

using namespace std; 

class someclass 
{ 
public: 

    int DoIt(float f, std::string s1) 
    { 
     return 0; 
    } 

    int test(boost::function<int(float, std::string)> funct) 
    { 
     return funct(5.0, "hello"); 
    } 

    void caller() 
    { 
     cout << test(boost::bind(&someclass::DoIt, this, _1, _2)) << endl; 
    } 
}; 

int main() { 
    someclass s; 
    s.caller(); 
} 
+0

Спасибо, что сделали это - маркировано как ответ после того, как таймер – Rajeshwar

+0

Нет проблем Rajeshwar , вы могли бы отметить этот ответ как правильный? Это знак галочки слева. Благодарю. –

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