2011-10-17 41 views
5

Çalıştığım bir ödevde kafamı bu problemle karşı karşıya getiriyordum ve hiç işe yaramayacak gibi görünmüyor. Ne yapmaya çalıştığımı göstermek için küçük bir test dersi yazdım ve umarım birileri ne yapmam gerektiğini açıklayabilir.İşlev Şablon sınıfının üye işlevine işaretçi? (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; 
} 

-ve burada asıl var:

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

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

    cin.get(); 
    return 0; 
} 

-ve son olarak, benim derleyici hataları (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>   ] 

Umarım, Tester sınıfında açıklamalarım Ben ne söyleyecektir yapmaya çalışıyorum. Buna bakmak için zaman ayırdığınız için teşekkür ederiz!

+0

emin olun ayrıca bkz uygunsa ödev etiketini ekleyin. Ayrıca, boost :: bind''e, özellikle de 'boost :: mem_fn''e bir göz atın. –

cevap

10

Üye işlev göstergesini doğru olarak çağırmıyorsunuz; pointer-to-member operator adı verilen özel bir operatörün kullanılmasını gerektirir. Açıkça nesne eklemek gerekir

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

Neredeyse doğru, ancak bir çift köşeli ayraç eksik. – UncleBens

+0

Oy, gerçekten. Teşekkür ederim! –

+0

Çok teşekkür ederim! – TNTisCOOL

1

bir işaretçi-to-üye-fonksiyonu artı örnek pointer, ihtiyacınız ->* sözdizimi, bakımı operatör önceliği üzerinden üye işlevini çağırmak için mesajı:

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

C++ FAQ

+0

Çalışıyor! Çok teşekkür ederim. Tüm bunları anlamaya çalışmak çirkin söz dizimi dolu bir kabusdu. – TNTisCOOL

1

:

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

Çalışıyor! Teşekkür ederim! – TNTisCOOL

İlgili konular