2015-07-29 4 views
2

Код должен объяснить мою сложность. Хотя сам код совершенно бессмыслен, я планирую добавлять контейнеры в MyClass и использовать алгоритмы с функциями-членами.Создайте функцию unary_function для нестатической функции-члена

#include <cstdlib> 
#include <algorithm> 
#include <functional> 

using namespace std; 

class MyClass 
{ 
    public: 
     MyClass() { a = 0; } 
     ~MyClass() {} 
    private: 
     int a; 
     bool tiny_test (int); 
     int Func(); 
}; 

bool MyClass::tiny_test (int b) 
{ 
    return a == b; 
} 

int MyClass::Func() 
{ 
    // does not compile 
    (mem_fun(&MyClass::tiny_test))(this); 

    // commented below is another attempt, also no success 
    //mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test); 
    //tmp_functor(this); 
    return 0; 
} 

int main(int argc, char** argv) 
{ 
    return 0; 
} 

Большое спасибо! Btw, я не использую статическую функцию-член, просто потому, что считаю, что она должна работать для нестатических функций-членов. P.S. Эрик, Jarod42, спасибо за быстрые ответы!

+0

Обратите внимание, что 'станд :: mem_fun' устарела, так как C++ 11 и удаляется в C++ 17 (в пользу' станд :: mem_fn' и ' станд :: bind'). – Jarod42

ответ

0
bool MyClass::tiny_test (int b) 
{      // ^^^^^ You missed this argument 
    return a == b; 
} 

Попробуйте это:

// Supply one more argument. E.g., 3 
(mem_fun(&MyClass::tiny_test))(this, 3); 
Смежные вопросы