6

I've been butting my head against this problem in an assignment I've been working on, and can't seem to get it to work at all. I wrote a little test class to demonstrate what I'm trying to do, and hopefully someone can explain what I need to do.

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

-and here's main:

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

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

    cin.get();
    return 0;
}

-and lastly, my compiler errors (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>          ]

Hopefully, my comments in the Tester class will tell you what I'm trying to do. Thank you for taking the time to look at this!

1
  • Make sure to add the homework tag if it is appropriate. Also, take a look at boost::bind, specifically boost::mem_fn. Commented Oct 17, 2011 at 20:16

3 Answers 3

11

You're not calling the member function pointer corrently; it requires the use of a special operator called the pointer-to-member operator.

template <typename T>
inline void Tester<T>::doFcn( FcnPtr fcn )
{
    (this->*fcn)( this->data );
    //   ^^^
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi guys, trying this code render me with 2 strange errors, in OS X and NetBeans Undefined symbols for architecture x86_64: "Tester<int>::function()", referenced from: _main in main.o "Tester<int>::Tester(int)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) , Any ideas?
2

To call a member function via a pointer-to-member-function plus instance pointer, you need the ->* syntax, minding operator precedence:

(this->*fcn)(data);

1 Comment

It works! Thank you so much. Trying to figure all that out was a nightmare full of ugly syntax.
1

You need to explicitly add the object you message:

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

see also the C++ FAQ

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.