Loading...
Pages: [1]   Go Down
Author Topic: C++ Problem with Wire.OnReceive()  (Read 519 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: 15
Posts: 1009
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

Pages: [1]   Go Up
Print
 
Jump to: