2011-10-17 7 views
5

मैं इस समस्या के खिलाफ अपने सिर को एक असाइनमेंट में बिता रहा हूं जिस पर मैं काम कर रहा हूं, और ऐसा लगता है कि यह बिल्कुल काम नहीं कर रहा है। मैंने यह दिखाने के लिए एक छोटी परीक्षा कक्षा लिखी कि मैं क्या करने की कोशिश कर रहा हूं, और उम्मीद है कि कोई मुझे समझा सकता है कि मुझे क्या करना है।टेम्पलेट वर्ग के सदस्य फ़ंक्शन के लिए फ़ंक्शन पॉइंटर? (सी ++)

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

-और यहाँ मुख्य है:

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

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

    cin.get(); 
    return 0; 
} 

-और अंत में, मेरी संकलक त्रुटियों (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>   ] 

उम्मीद है, परीक्षक कक्षा में मेरी टिप्पणी आपको बता मैं क्या 'होगा मैं करने की कोशिश कर रहा हूँ। इसे देखने के लिए समय निकालने के लिए धन्यवाद!

+0

करना सुनिश्चित करें यदि यह उचित है तो होमवर्क टैग जोड़ें। साथ ही, 'बूस्ट :: बाइंड' पर विशेष रूप से 'boost :: mem_fn' देखें। –

उत्तर

10

आप सदस्य फ़ंक्शन पॉइंटर को मूल रूप से कॉल नहीं कर रहे हैं; इसे pointer-to-member operator नामक एक विशेष ऑपरेटर के उपयोग की आवश्यकता है।

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

लगभग सही है, लेकिन ब्रैकेट की एक जोड़ी गायब है। – UncleBens

+0

ओई, वास्तव में। धन्यवाद! –

+0

आपको बहुत बहुत धन्यवाद! – TNTisCOOL

1

एक सूचक-टू-सदस्य-समारोह प्लस उदाहरण सूचक, आप की जरूरत ->* वाक्य रचना, काम से काम कर ऑपरेटर पूर्वता के माध्यम से एक सदस्य समारोह कॉल करने के लिए संदेश:

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

यह भी देखना C++ FAQ

+0

यह काम करता है! बहुत बहुत धन्यवाद। यह सब समझने की कोशिश कर रहा है कि बदसूरत वाक्यविन्यास से भरा एक दुःस्वप्न था। – TNTisCOOL

1

आप स्पष्ट रूप से वस्तु आप जोड़ने की जरूरत:

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

यह काम करता है! धन्यवाद! – TNTisCOOL