如何初始化类成员的函数指针?

时间:2016-11-16 07:22:16

标签: c++ function pointers compilation

我想传递一个函数指针以及fnptr_t类型。但它是Box类的成员。当我编译此代码时,我收到此错误。这是什么意思,我该如何解决?

#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;

}

错误:

main.cpp:26:26: error: cannot convert 'void (Box::*)(direction*)' to 'fnptr_t {aka void (*)(direction*)}' in initialization                                                                         
  fnptr_t ptr = &Box::fall;    

0 个答案:

没有答案
相关问题