Pages: [1]   Go Down
Author Topic: C++ Problem with Wire.OnReceive()  (Read 1053 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,
I'm using Eclipse with Arduino plugin and I've problem with Wire.OnReceive function:

This is my code:
Code:
void AuroraLibrary::_init_wire_link()
{
Wire.begin(WIRE_LINK);
Wire.onReceive(_on_wire_message_recevied);
}
my function _on_wire_message_recevied is definited in header and in file .cpp
Code:
void AuroraLibrary::_on_wire_message_recevied(int howMany)
But when i compile i've got this error:


ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&AuroraLibrary::_on_wire_message_recevied'   AuroraLibrary.cpp   /AuroraFirmware/lib/AuroraLibrary   line 106   C/C++ Problem


I also try to put this but i've same problem...

Code:
void AuroraLibrary::_init_wire_link()
{
Wire.begin(WIRE_LINK);
Wire.onReceive(&_on_wire_message_recevied);
}


Thanks Tommaso
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Every method needs an object to work with. If you just give the name of the method, the onReceive method doesn't know what object to apply the method to.

You could do some messy things with pointer to member stuff, or you could make a static member of AuroraLibrary that figures out which instance of the class to use (the constructor could assign a static pointer to "this") and does stuff to it.

Code:
#include <Wire.h>
class myclass {
  static myclass* classToUse;

  int randomdatayay;
public:
  myclass() {
    classToUse = this;
  }

  void init() {
    Wire.begin();
    Wire.onReceive(onMessageReceive);
  }
  void randommethodyay() {

  }
  static void onMessageReceive(int howmany) {
    classToUse->randomdatayay = howmany;
    classToUse->randommethodyay();
  }
};
myclass* myclass::classToUse = NULL;
Logged

Portugal
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi WizenedEE,

I'm having this same problem right now.

I'm using ICSC library (registerCommand method) inside another library I've made.

When I'm using function Callback (second argument on registerCommand method), I receive the same error that tgiachi received.

So I tried your solution but I can't figure out how to adapt your example properly.

May you explain it better?

Thank you.
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 6
Posts: 372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi!

The compiler is telling you exactly what the problem is. The error message is perfect ;-) If you check the specification of onReceive() you will see that it takes a callback function as parameter. Not an object and not a member function.

Code:
    void onReceive( void (*)(int) );

Define _on_wire_message_recevied() as static and it will compile BUT remember that there is no context ("this" is not valid in a static member function). A static member function cannot reference member variables.

There is an example sketch in arduino/libraries/Wire/examples. See slave_receiver.ino.

Cheers!
Logged

Pages: [1]   Go Up
Jump to: