1

Possible Duplicate:
How do you pass a member function pointer?

So I've been writing some libraries for Arduino to trim down a rather large sketch I've been working on. Everything is working so far, except for in the constructor in one of my classes. I use it to initialise any variables, pin modes and attaching interrupts. The problems come in when I try to attach interrupts, I have the functions declared and defined in my class, and whether I attach them in the Arduino sketch in the setup() block, or if I do it in my library in the constructor, I get this error:

argument of type 'void (RotaryEncoders::)()' does not match 'void (*)()'

The basic structure of the code is as follows:

RotaryEncoders::RotaryEncoders() {
     //Initialise some variables
     //Set up some pin modes
     attachInterrupt(2, doRedEncoder, CHANGE);
}

void RotaryEncoders::doRedEncoder() {
     //Some code, blah blah blah
}

I assume that when you do this purely within the Arduino IDE, the tools do something to the functions in memory to make it work properly. I think the solution is simple but I don't see it :S Thanks :D

Community
  • 1
  • 1
  • If `doRedEncoder` doest not rely on your class attributes or functions, you can simply declare it as `static`, that will do the trick – Zoneur Oct 19 '12 at 14:05
  • @Zoneur Arduino programming is a lot more procedural than object oriented(reserved only for libraries). Others just write functions like in pure C. – Aniket Inge Oct 19 '12 at 14:07

1 Answers1

1

Function pointer to a member-function is a bad idea because these functions usually are used by objects.

Two ways to solve it:

Solution1: have an external function(non-member function)(regular C-style function) to do things for you.

Solution2: Look at this

Good luck.

Community
  • 1
  • 1
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Not only bad idea ... it's illegal and may result in UB (**Undefined Behavior**). – iammilind Oct 19 '12 at 14:08
  • @iammilind a lot of things that're UB are COMPILER - DEPENDENT. Since Arduino uses the same compiler(gcc), certain UBs are eliminated. – Aniket Inge Oct 19 '12 at 14:11