-4

Trying to make LCD screen library for Arduino. Made a class "ScreenHandlerClass". This has S1_stat() and S2_stat() functions that will write different things on LCD screen. There's a "statScreenPointer", that I'm trying to make to call functions, however it does not work properly.

I tried to follow this guide: Calling Member Function Pointers That's the closest solution to my problem. I tried:

this->*statScreenPointer

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

Other I tried: 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'

CODE:

// 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
}


Gábor
  • 3
  • 2

2 Answers2

0

The function call operator () has higher precedence than the dereference operator *. That means you need parenthesis to call a member-function pointer:

(this->*statScreenPointer)();
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • working good, thanks for the answer and explanation. would have never guessed the correct syntax. – Gábor May 10 '19 at 22:16
0

The syntax you want is (as explained in your linked question):

(this->*statScreenPointer)();
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60