从C ++中的成员函数数组获取结果

Get result from array of member functions in c++

这是Arduino程序(C++)的一部分。单片机是ESP32.

我定义了一个class。我在其中创建了一个成员函数数组。

class IRDN_Padidar_Zone
{
bool callAnimation();  
uint16_t Animate_None(enum_Effect inOut);
uint16_t Animate_Print(enum_Effect inOut);
uint16_t Animate_Print_Random(enum_Effect inOut);
uint16_t Animate_Print_Barfak(enum_Effect inOut);
uint16_t Animate_Print_Laser(enum_Effect inOut);
typedef  uint16_t (IRDN_Padidar_Zone::*animatFunction)(enum_Effect);
animatFunction animations[] = {&IRDN_Padidar_Zone::Animate_None,&IRDN_Padidar_Zone::Animate_Print,&IRDN_Padidar_Zone::Animate_Print_Random,&IRDN_Padidar_Zone::Animate_Print_Barfak,&IRDN_Padidar_Zone::Animate_Print_Laser};
};

bool IRDN_Padidar_Zone::callAnimation(enum_Effect state)
{
 uint16_t frameCounter = 0;
 frameCounter =  *animations[0](state);
 return true;
}

当我在另一个 class 成员函数 callAnimation() 中访问此数组时,出现以下错误:

error: must use '.*' or '->*' to call pointer-to-member function in 'animations[0] (...)', e.g. '(... ->* animations[0]) (...)'
       frameCounter =  *animations[0](state);

如果我将 typedef 和函数数组移动到 class 之外,如下所示:

class IRDN_Padidar_Zone
{
bool callAnimation();  
uint16_t Animate_None(enum_Effect inOut);
uint16_t Animate_Print(enum_Effect inOut);
uint16_t Animate_Print_Random(enum_Effect inOut);
uint16_t Animate_Print_Barfak(enum_Effect inOut);
uint16_t Animate_Print_Laser(enum_Effect inOut);
};

typedef  uint16_t (IRDN_Padidar_Zone::*animatFunction)(enum_Effect);
animatFunction animations[] = {&IRDN_Padidar_Zone::Animate_None,&IRDN_Padidar_Zone::Animate_Print,&IRDN_Padidar_Zone::Animate_Print_Random,&IRDN_Padidar_Zone::Animate_Print_Barfak,&IRDN_Padidar_Zone::Animate_Print_Laser};

错误变为:

error: too many initializers for 'uint16_t (IRDN_Padidar_Zone::* [0])(enum_Effect) {aka short unsigned int (IRDN_Padidar_Zone::* [0])(enum_Effect)}'
                                 Animate_From_Up,&IRDN_Padidar_Zone::Animate_From_Down,&IRDN_Padidar_Zone::Animate_From_UpDown,&IRDN_Padidar_Zone::Animate_Kerkerh,&IRDN_Padidar_Zone::Animate_Print_Rain};

我很困惑。我是 C++ 的业余爱好者。请帮助我。

非静态 class 方法需要一个对象来 运行。正如第一条错误消息所说,通过指向方法的指针调用非静态方法时,必须使用 .*->* 运算符。

此外,当用作 class 成员时,数组需要指定它有多少个元素,即使它有一个内联初始化器。

试试这个:

class IRDN_Padidar_Zone
{
public:
    bool callAnimation(enum_Effect state);  
    uint16_t Animate_None(enum_Effect inOut);
    uint16_t Animate_Print(enum_Effect inOut);
    uint16_t Animate_Print_Random(enum_Effect inOut);
    uint16_t Animate_Print_Barfak(enum_Effect inOut);
    uint16_t Animate_Print_Laser(enum_Effect inOut);

    typedef  uint16_t (IRDN_Padidar_Zone::*animatFunction)(enum_Effect);

    animatFunction animations[5] = {&IRDN_Padidar_Zone::Animate_None, &IRDN_Padidar_Zone::Animate_Print, &IRDN_Padidar_Zone::Animate_Print_Random, &IRDN_Padidar_Zone::Animate_Print_Barfak, &IRDN_Padidar_Zone::Animate_Print_Laser};
};

bool IRDN_Padidar_Zone::callAnimation(enum_Effect state)
{
    uint16_t frameCounter = (this->*(animations[0]))(state);
    return true;
}

Online Demo