Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing member functions of class in global function before declaring object
    primarykey
    data
    text
    <p>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. </p> <p>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. </p> <p>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).</p> <p>Below is the member function and global event handler:</p> <pre><code>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)); </code></pre> <p>}</p> <p>Member function:</p> <pre><code>void CommunicationModuleUSB::SendBuffer(uint8_t * Buffer, int Size){ connection-&gt;write(Size,(uint8_t*)&amp;Buffer); } </code></pre> <h1>Update</h1> <p>With the reply of Daniel (thank you!) i changed the member function in the header file and cpp file to static as followed:</p> <pre><code> static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size); </code></pre> <p>And the function is called in the global eventhandler as followed:</p> <pre><code>// 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)); } </code></pre> <p>Only now i get the following error when i compile:</p> <p>C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.h:26: error: extra qualification 'CommunicationModuleUSB::' on member 'SendBuffer.</p> <p>Does anybody have a idea who to solve that?</p> <h1>Update 2</h1> <p>Thanks again Daniel for your reply!</p> <p>I have changed the member function with your feedback. But now i get the following error:</p> <p>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</p> <p>I have made the Connection variable static in the header file. Bellow is the header file and the function deffenition form the cpp file.</p> <p>Do you (or someone else) have any clue? All suggestions are welcome!</p> <p>Header file:</p> <pre><code>#include "CommunicationModule.h" #include &lt;SPI.h&gt; #include &lt;Adb.h&gt; 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: }; </code></pre> <p>The function decleration in the cpp file:</p> <pre><code>static void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){ connection-&gt;write(Size,(uint8_t*)&amp;Buffer); } </code></pre> <p>And the call in the global function:</p> <pre><code>CommunicationModuleUSB::SendBuffer(data, sizeof(data)); </code></pre> <h1>Update 3</h1> <p>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.</p> <p>The compiler error that i get is as followed: C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference to <code>CommunicationModuleUSB::connection' C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:79: undefined reference to</code>CommunicationModuleUSB::connection' CommunicationModuleUSB\CommunicationModuleUSB.cpp.o: In function <code>CommunicationModuleUSB::Connect()': C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference to</code>CommunicationModuleUSB::connection' C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModuleUSB/CommunicationModuleUSB.cpp:53: undefined reference to `CommunicationModuleUSB::connection'</p> <p>The connection variable is declared in the header file as followed:</p> <pre><code>// Adb connection made this static....(is this right? static Connection * connection; </code></pre> <p>The variable is used in the following member functions:</p> <pre><code>void CommunicationModuleUSB::SendBuffer(uint8_t* Buffer, int Size){ connection-&gt;write(Size,(uint8_t*)&amp;Buffer); } </code></pre> <p>And is used in the following global event handler function:</p> <pre><code>// 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)); } </code></pre> <p>Doe anybody have a suggestion how to solve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload