2

The code describes two classes that implement callback function, the function must should be member function in the class parameter that passed in template. Below the code i attached the relevent error message i get.

a.h

template <class CLASSNAME>
class a
{
public:
    typedef void (CLASSNAME::*myFunction)();

    a(CLASSNAME& myObject, myFunction callback) :
    m_myObject(myObject)
    {
        m_myFuntion = callback;
    }

    void update()
    {
        (m_myObject).*(m_myFuntion);
    }

    myFunction m_myFuntion;
    CLASSNAME& m_myObject;
};

dummy.h

#include <stdio.h>

class dummy
{
public:
    dummy()
    {
        var = 14;
    }


    void func()
    {
        printf("func!!");
    }

    int var;
};

main.cpp

#include <cstdlib>
#include "a.h"
#include "dummy.h"


void main()
{
    dummy dum;

    a<dummy> avar(dum, &(dummy::func));

    avar.update();

    system("pause");
}

i am trying to implement the callback function and i get the following error message:

C2298 missing call to bound pointer to member function  

what the problem is?

user11001
  • 372
  • 3
  • 14

2 Answers2

6

You have a lot of parentheses, they're just not in the right place. The correct syntax for calling a pointer-to-member function is:

void update()
{
    (m_myObject.*m_myFuntion)();
}
Barry
  • 286,269
  • 29
  • 621
  • 977
2

You are using parentheses in the wrong places:

This:

 a<dummy> avar(dum, &(dummy::func));

should be this:

 a<dummy> avar(dum, &dummy::func);

And this:

(m_myObject).*(m_myFuntion);

should be:

(m_myObject.*m_myFuntion)();

Live Example

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45