I have an event handler function and in that function there is a call to a member function of a class. The event handler function is declared in the class cpp file but not part of the class, it's no a member function.
When i compile the code the compiler says that the function is note in the scope, because it's calling a member function within the global event handler function.
My question is as followed: is there a way to use a meber function in a global function? (The object is created first on runtime).
Below is the member function and global event handler:
Global event handler:
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
Serial.println("In data recieve handler");
Serial.println("Data recieved: ");
Serial.println(data[0]);
Serial.println(data[1]);
char a = data[0];
char b = data[1];
Serial.println(a);
Serial.println(b);
//uint16_t data2 = data;
// Member function of USBCommunicator class
SendBuffer(data, sizeof(data));
}
Member function:
void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){
connection->write(Size,(uint8_t*)&Buffer);
}
Update
With the reply of Daniel (thank you!) i changed the member function in the header file and cpp file to static as followed:
static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size);
And the function is called in the global eventhandler as followed:
// Event handler for shell connection; called whenever data sent from Android to Microcontroller
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
Serial.println("In data recieve handler");
Serial.println("Data recieved: ");
//Serial.println(*data);
Serial.println(data[0]);
Serial.println(data[1]);
char a = data[0];
char b = data[1];
Serial.println(a);
Serial.println(b);
//uint16_t data2 = data;
CommunicationModuleUSB::SendBuffer(data, sizeof(data));
}
Only now i get the following error when i compile:
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.h:26: error: extra qualification 'CommunicationModuleUSB::' on member 'SendBuffer.
Does anybody have a idea who to solve that?
Update 2
Thanks again Daniel for your reply!
I have changed the member function with your feedback. But now i get the following error:
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB\CommunicationModuleUSB.cpp:77: error: cannot declare member function 'static void CommunicationModuleUSB::SendBuffer(uint8_t*, int)' to have static linkage
I have made the Connection variable static in the header file. Bellow is the header file and the function deffenition form the cpp file.
Do you (or someone else) have any clue? All suggestions are welcome!
Header file:
#include "CommunicationModule.h"
#include <SPI.h>
#include <Adb.h>
class CommunicationModuleUSB : public CommunicationModule
{
public:
CommunicationModuleUSB();
int Connect();
void Send();
int CheckConnection();
void Recieve();
static void SendBuffer(uint8_t* Buffer, int Size);
void RecieveBuffer(char Buffer[], int Size);
// Adb connection made this static....(is this right?
static Connection * connection;
// Elapsed time for sensor sampling
long lastTime;
private:
};
The function decleration in the cpp file:
static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){
connection->write(Size,(uint8_t*)&Buffer);
}
And the call in the global function:
CommunicationModuleUSB::SendBuffer(data, sizeof(data));
Update 3
I have updated te code with the help of Daniel, the only problem that i have now is that the Connection variable that is declared in the class is not in the scope anymore.
The compiler error that i get is as followed:
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference to CommunicationModuleUSB::connection'
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference to
CommunicationModuleUSB::connection'
CommunicationModuleUSB\CommunicationModuleUSB.cpp.o: In function CommunicationModuleUSB::Connect()':
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference to
CommunicationModuleUSB::connection'
C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference to `CommunicationModuleUSB::connection'
The connection variable is declared in the header file as followed:
// Adb connection made this static....(is this right?
static Connection * connection;
The variable is used in the following member functions:
void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){
connection->write(Size,(uint8_t*)&Buffer);
}
And is used in the following global event handler function:
// Event handler for shell connection; called whenever data sent from Android to Microcontroller
void adbEventHandler(Connection * connection, adb_eventType event, uint16_t length, uint8_t * data)
{
Serial.println("In data recieve handler");
Serial.println("Data recieved: ");
Serial.println(data[0]);
Serial.println(data[1]);
char a = data[0];
char b = data[1];
Serial.println(a);
Serial.println(b);
CommunicationModuleUSB::SendBuffer(data, sizeof(data));
}
Doe anybody have a suggestion how to solve this?
Member function is a member function and that is for a reason. You are calling
SendBuffer()
as if it was ordinary function defined in a global scope, which it is not. You can call member function in two ways.First: You create a instance of a class and then you call the method:
Second: You make the method
static
so the signature is as follows:So declaration would look like so:
and your definition of the function:
Now you can call it like this:
BUT this has more implications. Making the method static allows it to access only static member variables of the class as it does not belong to any particular object. This, however, makes sense as calling a method that belongs to a particular object is just the same as calling
eat()
method ofCarrot
that doesn't yet exist.