2016-06-21 63 views
0

我有一个包含两个相似的非静态函数的类,我想分配上飞他们中的一个函数指针:C++的Arduino:一个函数分配给函数指针

啊:

class A{ 
    private: 
    void (*funcPtr)(int); 
    void do_sth(int i); 
    void do_sth_else(int i); 
} 

A.cpp:

A::A(int i){ 
    if(i == 1) 
     A::funcPtr = A::do_sth; // or &A::do_sth; 
    else 
     A::funcPtr = A::do_sth_else; // or &A::do_sth_else; 
} 

,但我得到这样的错误:

error: cannot convert 'A::do_sth' from type 'void (A::)(int)' to type 'void (*)(int)' 

我读了多个类似的问题,但不能将他们的解决方案应用于我的问题。

+0

考虑到Arduino的内存限制,函数指针可能有点过于复杂。如果编译器可以处理它,为什么不使用[lambda](http://stackoverflow.com/questions/24352785/c-closure-to-pass-member-function-as-normal-function-pointer)? –

回答

1

这些是成员函数。通过使用类名限定它,可以使funcPtr成为一个成员指针,或者使do_sth(),do_sth_else()为静态成员函数。我建议在获取地址时在函数名前面使用&