Compiler Error 304

"no instance of overloaded function "mbed::Serial::attach" matches the argument list" in file "drv/serial/buf/SerialBuf.cpp", Line: 142, Col: 37

  • This error may occur when trying to attach InterruptIn::rise or InterruptIn::fall to a function that is a member of a class. Passing a "this" pointer may resolve the error.

This is the right way to attach InterruptIn to member functions

#include "mbed.h"

class Example
{
    Example(PinName p);
    

    int foo;
     InterruptIn MyInterrupt;
    void isr ();
    
    };
    
Example::Example(PinName p) : MyInterrupt(p) 
{
  foo = 0;
  
  //The right way for functions that are class members
  MyInterrupt.fall(this, &Example::isr);
  
  //This gives Error 304 because the InterruptIn::rise overloaded for member functions needs two arguments
  //The second argument is a pointer to a function
  //The first argument is a "this" pointer so the compiler knows which object the function pointer belongs to
  MyInterrupt.rise( &Example::isr)  

  //this way only works if isr() is not a member function
  //will give Error 504-D and Error 304 otherwise
  MyInterrupt.rise( &isr);
}   
        
void Example::isr ()
{
    foo++;
}

NOTE: error also pops up if you try and pass an unsigned char array to I2C.write()


All wikipages