【问题标题】:Getting address of class member function and calling it from pointer [closed]获取类成员函数的地址并从指针调用它[关闭]
【发布时间】:2019-09-28 14:11:09
【问题描述】:

尝试为 Arduino 制作 LCD 屏幕库。 做了一个类“ScreenHandlerClass”。这具有 S1_stat() 和 S2_stat() 函数,它们将在 LCD 屏幕上写入不同的内容。 有一个“statScreenPointer”,我正在尝试调用函数,但是它不能正常工作。

我尝试遵循本指南: Calling Member Function Pointers 这是最接近我的问题的解决方案。 我试过了:

this->*statScreenPointer

Error compiling project sources
ScreenHandler.cpp: 14:26: error: invalid use of non-static member function
   this->*statScreenPointer

我试过的其他: this->*statScreenPointer()

Error compiling project sources
ScreenHandler.cpp: 14:27: error: must use '.*' or '->*' to call pointer-to-member function in '((ScreenHandlerClass*)this)->ScreenHandlerClass::statScreenPointer (...)', e.g. '(... ->* ((ScreenHandlerClass*)this)->ScreenHandlerClass::statScreenPointer) (...)
   this->*statScreenPointer()
Build failed for project 'v1'

代码:

// ScreenHandler.h

#ifndef _SCREENHANDLER_h
#define _SCREENHANDLER_h

#include "arduino.h"
#include "debug.h"
#include "vezerles.h"
#include "EncoderHandler.h"
#include <LiquidCrystal_I2C.h>

extern EncoderHandlerClass encoder;
extern LiquidCrystal_I2C lcd;

enum screenType {
    S1,
    S2
};

extern screenType screen;

class ScreenHandlerClass
{
private:
    void logic();
    void (ScreenHandlerClass::*statScreenPointer)();

public:
    ScreenHandlerClass();
    void init();
    void handle();
    void S1_stat();
    void S2_stat();
};

#endif

// ScreenHandler.cpp
#include "ScreenHandler.h"

screenType screen;

ScreenHandlerClass::ScreenHandlerClass() {}

void ScreenHandlerClass::init() {

    statScreenPointer = &ScreenHandlerClass::S1_stat;
    this->*statScreenPointer; // ----> how to call this properly?
    lcd.setCursor(0, 1);
    lcd.print("init"); // this is DISPLAYED
}

void ScreenHandlerClass::handle()
{
    logic();
}

void ScreenHandlerClass::logic()
{
    // some logic for lcd screen switching
}

void ScreenHandlerClass::S1_stat() {
    lcd.setCursor(0, 0);
    lcd.print("S1_stat"); // this is NOT DISPLAYED
}

void ScreenHandlerClass::S2_stat() {
    // some other text for lcd
}
// v1.ino
#include "debug.h"
#include "global.h"
#include <TimerOne.h>                  
#include <LiquidCrystal_I2C.h>          
#include "MillisTimer.h"
#include "vezerles.h"
#include "lcd.h"
#include "EncoderHandler.h"
#include "ScreenHandler.h"

extern EncoderHandlerClass encoder;
ScreenHandlerClass scrh;
LiquidCrystal_I2C lcd(0x3F, 20, 4);

void setup() {
    Serial.begin(9600);
    encoder.initButton(PIND, PD4, 500);
    lcd.init();
    lcd.backlight();
    scrh.init();

}
void loop() {
    // some code
}


【问题讨论】:

  • #define _SCREENHANDLER_h 该标识符是保留的。通过定义它,您的程序将具有未定义的行为。您应该使用另一个标头保护。
  • 请提供minimal reproducible example(强调minimal),以便在此处按要求重现您的问题。
  • &eerorika - 感谢您指出这一点。会改变的

标签: c++ arduino function-pointers member-function-pointers


【解决方案1】:

您想要的语法是(如您的链接问题中所述):

(this->*statScreenPointer)();

【讨论】:

  • 谢谢!现在运行良好。
【解决方案2】:

函数调用运算符() 的优先级高于解引用运算符*。这意味着您需要括号来调用成员函数指针:

(this->*statScreenPointer)();

【讨论】:

  • 工作良好,感谢您的回答和解释。永远猜不到正确的语法。
猜你喜欢
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多