error: invalid use of non-static member function

Hi, I am trying to implement a library in C++ in order to use some motors of mine. These motors have encoders, so I'm using interrupts to read the encoder signal. Here's how the Encoder class is implemented:

class Encoder {

  const static int ANTI_CLOCKWISE = 1;
  const static int CLOCKWISE      = 0;
  const static int COUNTS         = 560;
  const static int OFFSET         = 11;

  Encoder(byte pin);
  void Setup();
  void DoEncoder();
  void SetRotation(int rotation);
  void ResetPos();
  volatile long GetPos();


  int rotation      = ANTI_CLOCKWISE;
  byte pin;
  volatile long pos = 0;

Compilation is resulting in the following message:

/home/pessoal/Arduino/libraries/neato/neato.cpp: In member function 'void Encoder::Setup()':
/home/pessoal/Arduino/libraries/neato/neato.cpp:9:70: error: invalid use of non-static member function
   attachInterrupt(digitalPinToInterrupt(pin), this->DoEncoder, CHANGE);
exit status 1

Here are the definitions of functions Encoder::Setup() and DoEncoder():

void Encoder::Setup() {
  pinMode(pin, INPUT);
  attachInterrupt(digitalPinToInterrupt(pin), this->DoEncoder, CHANGE);

void Encoder::DoEncoder() {
  if (rotation == ANTI_CLOCKWISE)
    pos += 1;
    pos -= 1;

If anyone could help me figuring out why that error is ocurring, I'd be very grateful! I've already tried changing this->DoEncoder for DoEncoder and for this->DoEncoder(), but it didn't work as well.

It has been discussed before. It's very difficult to get a proper function pointer to a non-static class method (i.e. an instance method). See:

Suppose that you create more than one instance of your class. When the interrupt happens, which of the many instances of the class should handle the interrupt?

1 Like