1

I would like to pass a function pointer along with type fnptr_t. However it is a member of Box class. When I compile this code I get this error. What does this mean and how can I fix it?

#include <iostream>

using namespace std;

struct direction{
    int i;
};

typedef void (*fnptr_t)(struct direction *);

class Box
{
public:
    Box(int size = 3);
    ~Box(void);

    void fall(struct direction *d){printf("fnptr\n");}

};

int main()
{
    /* main.cpp:26*/ fnptr_t ptr = &Box::fall;

}

Error:

main.cpp:26:26: error: cannot convert 'void (Box::*)(direction*)' to 'fnptr_t {aka void (*)(direction*)}' in initialization                                                                         
  fnptr_t ptr = &Box::fall;    
William Good
  • 109
  • 1
  • 7
  • 1
    http://stackoverflow.com/questions/1485983/calling-c-class-methods-via-a-function-pointer – john elemans Nov 16 '16 at 07:24
  • or this: https://stackoverflow.com/questions/2402579/function-pointer-to-member-function or dozens more. Searching for `[cpp] pointer to member function` will yield many related questions. – WhozCraig Nov 16 '16 at 07:25

0 Answers0