2011-10-17 28 views
5

Mi sono scontrato con questo problema in un incarico a cui ho lavorato e non riesco a farlo funzionare. Ho scritto un piccolo corso di prova per dimostrare cosa sto cercando di fare, e spero che qualcuno possa spiegare cosa devo fare.Funzione Puntatore alla funzione membro della classe template? (C++)

//Tester class 
#include <iostream> 
using namespace std; 

template <typename T> 
class Tester 
{ 
    typedef void (Tester<T>::*FcnPtr)(T); 

private: 
    T data; 
    void displayThrice(T); 
    void doFcn(FcnPtr fcn); 

public: 
    Tester(T item = 3); 
    void function(); 
}; 

template <typename T> 
inline Tester<T>::Tester(T item) 
    : data(item) 
{} 

template <typename T> 
inline void Tester<T>::doFcn(FcnPtr fcn) 
{ 
    //fcn should be a pointer to displayThrice, which is then called with the class data 
    fcn(this->data); 
} 

template <typename T> 
inline void Tester<T>::function() 
{ 
    //call doFcn with a function pointer to displayThrice() 
    this->doFcn(&Tester<T>::displayThrice); 
} 

template <typename T> 
inline void Tester<T>::displayThrice(T item) 
{ 
    cout << item << endl; 
    cout << item << endl; 
    cout << item << endl; 
} 

e qui è principale:

#include <iostream> 
#include "Tester.h" 
using namespace std; 

int main() 
{ 
    Tester<int> test; 
    test.function(); 

    cin.get(); 
    return 0; 
} 

-e infine, i miei errori del compilatore (VS2010)

c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(28): error C2064: term does not evaluate to a function taking 1 arguments 
1>   c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(26) : while compiling class template member function 'void Tester<T>::doFcn(void (__thiscall Tester<T>::*)(T))' 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>   c:\users\name\documents\visual studio 2010\projects\example\example\tester.h(21) : while compiling class template member function 'Tester<T>::Tester(T)' 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>   c:\users\name\documents\visual studio 2010\projects\example\example\example.cpp(7) : see reference to class template instantiation 'Tester<T>' being compiled 
1>   with 
1>   [ 
1>    T=int 
1>   ] 

Speriamo che i miei commenti nella classe Tester vi dirà quello che ho' sto cercando di fare. Grazie per aver dedicato del tempo a guardare questo!

+0

assicurarsi di aggiungi il tag dei compiti a casa se è appropriato. Inoltre, dai un'occhiata a 'boost :: bind', in particolare' boost :: mem_fn'. –

risposta

10

Non si sta chiamando il puntatore della funzione membro correttamente; richiede l'uso di un operatore speciale chiamato pointer-to-member operator.

template <typename T> 
inline void Tester<T>::doFcn(FcnPtr fcn) 
{ 
    (this->*fcn)(this->data); 
    // ^^^ 
} 
+0

Quasi corretto, ma manca un paio di parentesi. – UncleBens

+0

Oy, davvero. Grazie! –

+0

Grazie mille! – TNTisCOOL

1

Per chiamare una funzione di membro tramite un puntatore-a-membro-funzione più puntatore esempio, è necessario il ->* sintassi, badando precedenza degli operatori:

(this->*fcn)(data); 
+0

Funziona! Grazie mille. Cercare di capirlo era un incubo pieno di brutte sintassi. – TNTisCOOL

1

È necessario aggiungere in modo esplicito l'oggetto che si messaggio:

(*this.*fcn)(this->data); // << '*this' in this case 

veda anche la C++ FAQ

+0

Funziona! Grazie! – TNTisCOOL

Problemi correlati