invalid use of non-static member function error

Hi, I am trying to create a library that uses the keypad library. I am not sure how to fix this, since changing the function lead to more errors.

sketch\latch.cpp: In member function 'void latch::begin(int)':

latch.cpp:10:38: error: invalid use of non-static member function

keypad.addEventListener(keypadEvent);

^

exit status 1
invalid use of non-static member function

-----ino file------
#include "latch.h"
latch doorlatch;

void setup(){
  doorlatch.begin(9600);
  }
 void loop(){
  doorlatch.main();
  }
-----cpp file-----
#include "latch.h"
#include "Arduino.h"

latch::latch():keypad( makeKeymap(keys), rowPins, colPins, Rows, Cols ) {
}

void latch::begin(int baudrate){
  Serial.begin(baudrate);
  Serial.println("Latch library created");
  keypad.addEventListener(keypadEvent); 
    
}

void latch::main(){
  keypad.getKey();
}
void latch::keypadEvent(KeypadEvent input){
  switch (keypad.getState()){
  case PRESSED:
  Serial.print("Enter: ");
  Serial.println(input);
  delay(10);

  }
}
------h file------
#include <Keypad.h>

#ifndef _latch_
#define _latch_

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class latch {
  public:
    latch();
    void keypadEvent(KeypadEvent input);
    void begin(int baudrate);
    void main();
    Keypad keypad;
  private:


    const byte Rows = 4;
    const byte Cols = 4;

    char keys[4][4] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}
    };

    byte rowPins[4] = {7, 6, 5, 4};
    byte colPins[4] = { 11, 10, 9, 8 };
};

#endif

Copy, paste, post the complete text of the error message.

Ive pasted the full error

You can't pass a pointer to a member function the same way as a regular function. See: Standard C++
However, you can use a 'static' member function this way.

When I change it to a static void function, it will cause more errors since the functions I call from the keypad library is non static. Below is the error when I changed it to static

sketch\latch.cpp:7:38: warning: cannot declare member function 'void latch::begin(int)' to have static linkage [-fpermissive]

static void latch::begin(int baudrate){

^

sketch\latch.cpp: In static member function 'static void latch::main()':

latch.cpp:15:3: error: invalid use of member 'latch::keypad' in static member function

keypad.getKey();

^

In file included from sketch\latch.cpp:1:0:

sketch\latch.h:18:12: note: declared here

Keypad keypad;

^

sketch\latch.cpp: At global scope:

sketch\latch.cpp:17:49: warning: cannot declare member function 'static void latch::keypadEvent(KeypadEvent)' to have static linkage [-fpermissive]

static void latch::keypadEvent(KeypadEvent input){

^

sketch\latch.cpp: In static member function 'static void latch::keypadEvent(KeypadEvent)':

latch.cpp:18:11: error: invalid use of member 'latch::keypad' in static member function

switch (keypad.getState()){

^

In file included from sketch\latch.cpp:1:0:

sketch\latch.h:18:12: note: declared here

Keypad keypad;

^

exit status 1
invalid use of member 'latch::keypad' in static member function

I imagine that the problem is in the updated code that you didn't post.

this is the updated code.

door.txt (1.36 KB)

Don't include the 'static' keyword in the function definition, just the declaration:

staticvoid latch::main() {

static void latch::keypadEvent(KeypadEvent input) {

Also, you're trying to access the instance member 'keypad' inside these static functions. The compiler has no way of knowing which instance to use.

Finally, in cases like this it's a lot easier for people to help you if you combine the .h, .cpp, and .ino files together and post it in code tags. That way we don't have to cut and paste in order to drop your code into the Arduino IDE and compile it. You can always split it into separate files once it's working. Here's your latest code formatted this way:

#include <Arduino.h>
#include <Keypad.h>

class latch {
  public:
    latch();
    static void keypadEvent(KeypadEvent input);
    void begin(int baudrate);
    static void main();
    Keypad keypad;

  private:
    const byte Rows = 4;
    const byte Cols = 4;

    char keys[4][4] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}
    };

    byte rowPins[4] = {7, 6, 5, 4};
    byte colPins[4] = { 11, 10, 9, 8 };
};


latch::latch(): keypad( makeKeymap(keys), rowPins, colPins, Rows, Cols ) {
}

void latch::begin(int baudrate) {
  Serial.begin(baudrate);
  Serial.println("Latch library created");
  keypad.addEventListener(keypadEvent);
}

static void latch::main() {
  keypad.getKey();
}

static void latch::keypadEvent(KeypadEvent input) {
  switch (keypad.getState()) {
    case PRESSED:
      Serial.print("Enter: ");
      Serial.println(input);
      delay(10);
  }
}

latch doorlatch;

void setup() {
  doorlatch.begin(9600);
}
void loop() {
  doorlatch.main();
}

thank you for the advice on sharing the code, I will definitely take note of that.

after making the changes you said, there's still errors which is probably because what you said.

Also, you're trying to access the instance member 'keypad' inside these static functions. The compiler has no way of knowing which instance to use.

can you please expand a bit more on this ?

errors I got

In file included from sketch\latch.cpp:1:0:

latch.h:14:5: error: expected primary-expression before 'void'

void keypadEvent(KeypadEvent input);

^

sketch\latch.h:14:5: warning: ISO C++ forbids declaration of 'type name' with no type [-fpermissive]

latch.h:14:5: error: '' is neither function nor member function; cannot be declared friend

latch.h:13:9: error: expected ';' at end of member declaration

friend:

^

sketch\latch.cpp: In member function 'void latch::begin(int)':

latch.cpp:10:38: error: invalid use of non-static member function

keypad.addEventListener(keypadEvent);

^

In file included from C:\Users\Wan Arif\Desktop\3rd Year\Group Project\Prototypes\DoorLatch\doorlatchprototype\testing\doorlatchprototype\doorlatchprototype.ino:1:0:

latch.h:14:5: error: expected primary-expression before 'void'

void keypadEvent(KeypadEvent input);

^

sketch\latch.h:14:5: warning: ISO C++ forbids declaration of 'type name' with no type [-fpermissive]

latch.h:14:5: error: '' is neither function nor member function; cannot be declared friend

latch.h:13:9: error: expected ';' at end of member declaration

friend:

^

exit status 1
expected primary-expression before 'void'

----main.ino----
#include "latch.h"
latch doorlatch;

void setup(){
  doorlatch.begin(9600);
  }
 void loop(){
  doorlatch.main();
  }

----h file----
#include <Keypad.h>

#ifndef _latch_
#define _latch_

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

class latch {

  public:

    latch();
    void begin(int baudrate);
    static void main();
    static void keypadEvent(KeypadEvent input);
    Keypad keypad;


  private:

    const byte Rows = 4;
    const byte Cols = 4;

    char keys[4][4] = {
      {'1', '2', '3', 'A'},
      {'4', '5', '6', 'B'},
      {'7', '8', '9', 'C'},
      {'*', '0', '#', 'D'}
    };

    byte rowPins[4] = {7, 6, 5, 4};
    byte colPins[4] = { 11, 10, 9, 8 };
};

#endif

----cpp file----

latch::latch():keypad( makeKeymap(keys), rowPins, colPins, Rows, Cols ) {
}

void latch::begin(int baudrate){
  Serial.begin(baudrate);
  Serial.println("Latch library created");
  keypad.addEventListener(keypadEvent);
    
}

void latch::main(){
  keypad.getKey();
}
void latch::keypadEvent(KeypadEvent input){
  switch (keypad.getState()){
  case PRESSED:
  Serial.print("Enter: ");
  Serial.println(input);
  delay(10);

  }
}
void latch::main(){
  keypad.getKey();
}

What is the purpose of calling getkey(), if you don't care what key was pressed, or even if a key was pressed?

Why is the keypad a member of the latch class? The keypad should be considered a shared resource, managed by the sketch. If an instance of the latch class needs to know that a key was pressed, it should provide a method that the sketch can use to tell it that.