Compilation error Arduino C++ object library - c++

I'm writing a C++ Arduino library for BlueTooth communication.
I implemented a mechanism in order to register a custom function to be executed when a certain byte comes from the BT connection. Pratically a sort of Java Hashmap.
Everything works fine but when I try to register a method of the library object in the constructor I got some compilation error which I cannot understand.
I'm using Arduino IDE 1.6.9 with Ubuntu 16.04 LTS 64bit. The board is an Arduino UNO.
Here below the code,
header:
#ifndef BlueHartwin_H
#define BlueHartwin_H
#include <SoftwareSerial.h>
#include <Streaming.h>;
#include <Arduino.h>
#define START_TX 254
#define STOP_TX 255
typedef void (* CmdFuncPtr) (); // this is a typedef to command functions
class BlueHartwin {
public:
BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate);
~BlueHartwin();
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean runCmd(byte id);
void setDataLength(byte dataLength);
byte getDataLength();
byte getIncomingByte();
void txSwitchOff(void);
void txSwitchOn(void);
bool isTx();
boolean available();
boolean read();
void write(byte data,boolean endLine);
private:
CmdFuncPtr setOfCmds[255];
byte mDataLength;
bool tx;
byte incomingByte;
};
#endif
body:
#include "BlueHartwin.h";
/*
* Reserved command IDs:
* 14,
* 15
*/
SoftwareSerial bluetooth(7,8);
BlueHartwin::BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate){
bluetooth = SoftwareSerial(bluetoothTx,bluetoothRx);
bluetooth.begin(baudRate);
for (int i=0;i<=255;i++) {
setOfCmds[i]=NULL;
}
tx=false;
registerCmd(14,&txSwitchOff);
registerCmd(15,&txSwitchOn);
}
BlueHartwin::~BlueHartwin(){
}
boolean BlueHartwin::registerCmd(byte id,CmdFuncPtr cmdFuncPtr){
if (setOfCmds[id]==NULL) {
setOfCmds[id]=cmdFuncPtr;
return true;
} else return false;
}
boolean BlueHartwin::unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr){
setOfCmds[id]=NULL;
return true;
}
boolean BlueHartwin::runCmd(byte id){
if (setOfCmds[id]!=NULL) {
setOfCmds[id]();
return true;
} else return false;
}
void BlueHartwin::setDataLength(byte dataLength) {
mDataLength=dataLength;
}
byte BlueHartwin::getDataLength(){
return mDataLength;
}
byte BlueHartwin::getIncomingByte(){
return incomingByte;
}
boolean BlueHartwin::isTx(){
return tx;
}
void BlueHartwin::txSwitchOff(void){
tx=false;
Serial<<"now tx is "<<tx<<endl;
}
void BlueHartwin::txSwitchOn(void){
tx=true;
Serial<<"now tx is "<<tx<<endl;
}
boolean BlueHartwin::available(){
return bluetooth.available();
}
boolean BlueHartwin::read(){
if (bluetooth.available()>0)
{
incomingByte=bluetooth.read();
Serial << "Incoming byte " << incomingByte<<endl;
return runCmd(incomingByte);
}
return false;
}
void BlueHartwin::write(byte data,boolean endLine){
if (tx) {
if (endLine) bluetooth << data << endl;
else bluetooth<<data;
}
}
error:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BlueHartwin::txSwitchOff' [-fpermissive]
registerCmd(14,&txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(14,&txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BlueHartwin::txSwitchOn' [-fpermissive]
registerCmd(15,&txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(15,&txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.
If I change in the constructor the rows:
registerCmd(14,&txSwitchOff);
registerCmd(15,&txSwitchOn);
to
registerCmd(14,&BlueHartwin::txSwitchOff);
registerCmd(15,&BlueHartwin::txSwitchOn);
I got this error:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(14,&BlueHartwin::txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(15,&BlueHartwin::txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.
Thanks in advance for help!

Mate, your txSwitchOn/Off methods are instance ones - they require a this to work fine.
On the other side, your CmdFuncPtr is the definition of a function that doesn't depend on anything.
How many such devices (to be supported by your library) is Arduino assumed to handle at any one time?
If it's a single one, then declare everything static, rename the constructot to "static void init() and the destructor to "static void disconnect()", etc. You will have the class acting as just a naming context for your functions (all static).

Related

callback in MCS6502 functions will not compile in arduino on esp32 but will compile in codeblocks c file

I know this is a noob question bit I am not familiar with c++ .
My understanding is that arduino code compiles in c++ and this is what causes the problem in compiling.
The code below will compile fine in codeblocks and will run correctly , however if I use the same code without any modifications then it will produce an error at compile time.
heres is the .ino file
#include "MCS6502.h"
int8_t ram [65535];
//////CALL BACK FUNCTION ///////////////////////////////////////////////
uint8_t readBytesFunction(uint16_t add) {
uint8_t tc = 5;
tc = ram[add];
return tc;
}
//////CALL BACK FUNCTION ///////////////////////////////////////////////
void writeBytesFunction(uint16_t add,uint8_t bb) {
}
void setup()
{
Serial.begin(115200);
Serial.println();
///CODE BELOW WILL COMPILE AND RUN IN CODEBLOCKS BUT WILL NOT COMPILE IN ARDUINO
/////////////////////////////////////////////////////////////////////
MCS6502ExecutionContext context;
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
MCS6502Reset(&context);
MCS6502Tick(&context); //use timings
MCS6502ExecNext(&context); //as fast as possible
}
these are the errors after compiling in arduino for esp32
MCS6502.ino: In function 'void setup()':
wahid_MCS6502:22:27: error: invalid conversion from 'uint8_t (*)(uint16_t)' {aka 'unsigned char (*)(short unsigned int)'} to 'MCS6502DataReadByteFunction' {aka 'unsigned char (*)(short unsigned int, void*)'} [-fpermissive]
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
^~~~~~~~~~~~~~~~~
MCS6502.h:91:33: note: initializing argument 2 of 'void MCS6502Init(MCS6502ExecutionContext*, MCS6502DataReadByteFunction, MCS6502DataWriteByteFunction, void*)'
MCS6502DataReadByteFunction readByteFn,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
MCS6502:22:46: error: invalid conversion from 'void (*)(uint16_t, uint8_t)' {aka 'void (*)(short unsigned int, unsigned char)'} to 'MCS6502DataWriteByteFunction' {aka 'void (*)(short unsigned int, unsigned char, void*)'} [-fpermissive]
MCS6502Init(&context, readBytesFunction, writeBytesFunction, NULL); // Final param is optional conte>
^~~~~~~~~~~~~~~~~~
MCS6502.h:92:34: note: initializing argument 3 of 'void MCS6502Init(MCS6502ExecutionContext*, MCS6502DataReadByteFunction, MCS6502DataWriteByteFunction, void*)'
MCS6502DataWriteByteFunction writeByteFn,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
exit status 1
invalid conversion from 'uint8_t (*)(uint16_t)' {aka 'unsigned char (*)(short unsigned int)'} to 'MCS6502DataReadByteFunction' {aka 'unsigned char (*)(short unsigned int, void*)'} [-fpermissive]
what do i need to do it compiles in arduino ??
apologies if some details are missing from this question but its my 1st ever time asking a question here.
many thanks
I'm guessing that you are using this library?
Looking at the signatures of the callback functions both require a final void* parameter which your callbacks are missing.
You need:
uint8_t readBytesFunction(uint16_t add, void*) {
uint8_t tc = 5;
tc = ram[add];
return tc;
}
void writeBytesFunction(uint16_t add,uint8_t bb, void*) {
}

ESP32: Compile errors when using IRAM_ATTR

I am sure I am doing something really stupid, but here goes..
I get a compile error when I use IRAM_ATTR for my ISR. I am compiling for ESP32.
Here is the definition of the ISR
void
IRAM_ATTR
detectsBlockOccupancy() {
....
ISR code..
...
}
And here is the first usage:
void setup() {
// Set Sensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(BDInterruptPin), detectsBlockOccupancy, RISING);
...
}
The error I get is as follows:
In function 'void setup()': Node0:226:90: error: invalid conversion
from 'int ()()' to 'void ()()' [-fpermissive]
attachInterrupt(digitalPinToInterrupt(BDInterruptPin), detectsBlockOccupancy, RISING);
^ In file included from
C:\Users\vibhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/esp32-hal.h:53:0,
from C:\Users\vibhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/Arduino.h:35,
from sketch\Node0.ino.cpp:1: C:\Users\vibhas\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\cores\esp32/esp32-hal-gpio.h:81:6:
note: initializing argument 2 of 'void attachInterrupt(uint8_t, void
()(), int)' void attachInterrupt(uint8_t pin, void ()(void), int
mode);
^
If I remove the IRAM_ATTR then it compiles fine. It seems to think that I am trying to pass a different type of function pointer.
What could I be doing wrong ?
Thanks
vibhas

Error: 'argument' does not name a type, supposed problems with .cpp and .h files

I read about this issue (seems quite common) for hours without finding any applicable solution to my situation. I understand that there might be a problem related to the included files and libraries, but I'm not actually able to find what's wrong.
It's an Arduino script that uses the MQTT client library. It works beautifully with the old PubSubClient.h lib.
Now I wanted to update the lib with a recent one. The function definitions are changed so I made the changes in the sketch and switched the lib in the Arduino/library directory, then restarted the Arduino IDE. I get lot of "error: 'argument' does not name a type" and I really don't know what to fix.
Here you can find the .h and .cpp files organization to understand what's happening.
//device.ino file
#include "device.h"
//device.h file
#ifndef DEVICE_H
#define DEVICE_H
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <jsmn.h>
#include <Arduino.h>
#include <pgmspace.h>
#include "mqtt.h"
#endif
mqtt.h file
#ifndef MQTT_H
#define MQTT_H
#include "device.h"
#endif
mqtt.cpp file
#include "mqtt.h"
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
Finally the errors the compiler throws out:
mqtt.cpp:19: error:
'mqtt_client' does not name a type
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
^
mqtt.cpp:20: error: 'mqtt_client' does not name a type
mqtt_client.setCallback(mqtt_callback);
^
sketch\mqtt.cpp: In function 'void mqtt_publish_mex(String, String, bool)':
mqtt.cpp:27: error: no matching function for call to 'PubSubClient::publish(String&, String&, bool&)'
if (mqtt_client.publish(topic, jmex, retained)) {
^
sketch\mqtt.cpp:27:54: note: candidates are:
In file included from sketch\Walvola.h:25:0,
from sketch\mqtt.h:4,
from sketch\mqtt.cpp:1:
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: candidate expects 2 arguments, 3 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: no known conversion for argument 1 from 'String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: no known conversion for argument 1 from 'String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: candidate expects 4 arguments, 3 provided
sketch\mqtt.cpp: In function 'void mqtt_log(String)':
mqtt.cpp:347: error: no matching function for call to 'PubSubClient::publish(const String&, String&)'
mqtt_client.publish(mqtt_controllers_topic_debug, json_string);
^
sketch\mqtt.cpp:347:70: note: candidates are:
In file included from sketch\Walvola.h:25:0,
from sketch\mqtt.h:4,
from sketch\mqtt.cpp:1:
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: boolean PubSubClient::publish(const char*, const char*)
boolean publish(const char* topic, const char* payload);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:130:12: note: no known conversion for argument 1 from 'const String' to 'const char*'
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: boolean PubSubClient::publish(const char*, const char*, boolean)
boolean publish(const char* topic, const char* payload, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:131:12: note: candidate expects 3 arguments, 2 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:132:12: note: candidate expects 3 arguments, 2 provided
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
C:\Users\Fabrizio & Dario\Documents\Arduino\libraries\pubsubclient\src/PubSubClient.h:133:12: note: candidate expects 4 arguments, 2 provided
In file included from sketch\mqtt.h:4:0,
from sketch\mqtt.cpp:1:
sketch\mqtt.cpp: In function 'void mqtt_callback(char*, byte*, unsigned int)':
mqtt.cpp:375: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
log(payload.payload_string());
^
sketch\Walvola.h:89:67: note: in definition of macro 'log'
#define log(mex) if (DEBUG) {Serial.println(walvola_time + "::" + mex);Serial.flush(); if(MQTT_DEBUG) {mqtt_log(walvola_time + "::" + mex);}}
^
mqtt.cpp:375: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
log(payload.payload_string());
^
sketch\Walvola.h:89:135: note: in definition of macro 'log'
#define log(mex) if (DEBUG) {Serial.println(walvola_time + "::" + mex);Serial.flush(); if(MQTT_DEBUG) {mqtt_log(walvola_time + "::" + mex);}}
^
mqtt.cpp:379: error: request for member 'payload_string' in 'payload', which is of non-class type 'byte* {aka unsigned char*}'
if (mqtt2JSONkvs(payload.payload_string())) {
^
mqtt.cpp:404: error: a function-definition is not allowed here before '{' token
{
^
mqtt.cpp:512: error: expected '}' at end of input
}
^
Più di una libreria trovata per "WiFiClient.h"
Usata: C:\Users\Fabrizio & Dario\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\libraries\ESP8266WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
Non usata: C:\Program Files (x86)\Arduino\libraries\WiFi
exit status 1
'mqtt_client' does not name a type
Thanks for the help!
when writing
WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
the first two lines are quite ok => you are declaring global variables named wifi_client and mqtt_client
The problem comes with the calls
mqtt_client.setServer(mqtt_server, MQTT_BROKER_PORT);
mqtt_client.setCallback(mqtt_callback);
You can declare variables at global scope, but you cannot call functions outside a function (unless is for a global variable initialization), this is why you get this error. The example you point is different as the variables are in a function, thus calling methodes on them just after is legal: you are in a function context.
So you need to move your calls in the main() function, for example, so that the code gets valid.
That said, putting global variables in the header could make you more trouble is you include you file from different cpp files, the compiler will complain about multiple definitions, but that is not (yet) the question here.

No matching function for call to SocketIOClient::SocketIOClient()

I'm trying to maintain a communication between node JS server and Arduino using SocketIO, I tried to test this example (arduinoChat) https://github.com/quentinpigne/socket.io-arduino-client and I get this error:
arduinochat.ino:6:16: error: no matching function for call to ‘SocketIOClient::SocketIOClient()’
arduinochat.ino:6:16: note: candidates are:
In file included from arduinochat.ino:4:0:
/home/nourhene/sketchbook/libraries/SocketIOClient/SocketIOClient.h:52:3: note: SocketIOClient::SocketIOClient(Client&)
SocketIOClient(Client& client);
^
/home/nourhene/sketchbook/libraries/SocketIOClient/SocketIOClient.h:52:3: note: candidate expects 1 argument, 0 provided
/home/nourhene/sketchbook/libraries/SocketIOClient/SocketIOClient.h:50:7: note: SocketIOClient::SocketIOClient(const SocketIOClient&)
class SocketIOClient {
^
/home/nourhene/sketchbook/libraries/SocketIOClient/SocketIOClient.h:50:7: note: candidate expects 1 argument, 0 provided
arduinochat.ino: In function ‘void setup()’:
arduinochat.ino:30:55: error: invalid conversion from ‘void (*)(EthernetClient, char*)’ to ‘void (*)(Client&, ArduinoJson::JsonArray&)’ [-fpermissive]
In file included from arduinochat.ino:4:0:
/home/nourhene/sketchbook/libraries/SocketIOClient/SocketIOClient.h:56:8: note: initializing argument 2 of ‘void SocketIOClient::setEventHandler(char*, void (*)(Client&, ArduinoJson::JsonArray&))’
void setEventHandler(char* eventName, void (*handler)(Client& client, JsonArray& data));
^
arduinochat.ino : 6 : 16 =>
SocketIOClient client;
SocketIOClient.h:52:3: =>
class SocketIOClient {
public:
SocketIOClient(Client& client);

C++ Boost::serialization "no matching function for call" to constructor of the class in my loading function's argument

My actual question is in the bold text below, but here's the context of my problem:
I'm trying out Boost::serialization for saving and restoring a player's state. I read the tutorial at http://www.boost.org/doc/libs/1_56_0/libs/serialization/doc/tutorial.html#simplecase, but I'm not sure how
boost::archive::text_iarchive
works.
I'm working on the assumption that the following lines
boost::archive::text_iarchive inArchive(playerfile);
inArchive >> target;
will initialize the target according to the playerfile's corresponding textfile?
I'm wrote the following functions with that assumption:
bool SavePlayerState(Player *target)
{
std::string fileName = (target->name) + ".playerfile";
std::ofstream playerfile(fileName);
if(!playerfile.is_open())
{
s_al_show_native_message_box(display, "SAVE FAILURE", "SAVE FAILURE", fileName + "could not be created/opened.",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return false;
}
boost::archive::text_oarchive outArchive(playerfile);
outArchive << target;
return true;
}
bool LoadPlayerState(std::string playerName, Player *target)
{
std::string fileName = playerName + ".playerfile";
std::ifstream playerfile(fileName);
if(!playerfile.is_open())
{
s_al_show_native_message_box(display, "LOAD FAILURE", "LOAD FAILURE", fileName + "could not be found/opened.",
NULL, ALLEGRO_MESSAGEBOX_ERROR);
return false;
}
boost::archive::text_iarchive inArchive(playerfile);
inArchive >> target;
return true;
}
They are called in main() for testing purposes:
int main(int argc, char *argv[])
{
//...
player = new Player(5,5); // There is a Player*player; declared elsewhere
LoadPlayerState("player", player); //Load the file with this name, target is player object
beings.push_back(player);
//The game's operations...
SavePlayerState(player);
delete player;
//...
return 0;
}
SavePlayerState(player); works as I expected, and I find that player.playerfile has been created in my directory. But LoadPlayerState("player", player); gives me the following error:
C:\Development\Libraries\boost\boost\serialization\access.hpp|132|error: no matching function for call to 'Player::Player()'|
I don't know why the constructor should have problems, or why Save works and not Load. I'm not trying to make a new Player object, just shape the existing target Player according to the archive. What do I need to change in order to make this work?
My player class:
#ifndef PLAYER_H_INCLUDED
#define PLAYER_H_INCLUDED
#include "being.h"
#include "extfile.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/vector.hpp>
class Player: public Being
{
friend class boost::serialization::access;
template<class PlayerArchive>
void serialize(PlayerArchive & par, const unsigned int version)
{
par & boost::serialization::base_object<Being>(*this);
par & active;
//par & various things
}
public:
Player(bool savedPlayer);
Player(int spawnXCell, int spawnYCell);
~Player();
//Functions
};
//Prototype to the save and Load state functions mentioned previously are found here.
#endif // PLAYER_H_INCLUDED
Here is my complete build messages log, if it helps:
>||=== Build: Debug in Roguelike (compiler: GNU GCC Compiler) ===|
>C:\Development\Libraries\boost\boost\serialization\access.hpp||In instantiation of 'static void boost::serialization::access::construct(T*) [with T = Player]':|
>C:\Development\Libraries\boost\boost\serialization\serialization.hpp|93|required from 'void boost::serialization::load_construct_data(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Libraries\boost\boost\serialization\serialization.hpp|158|required from 'void boost::serialization::load_construct_data_adl(Archive&, T*, unsigned int) [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Libraries\boost\boost\archive\detail\iserializer.hpp|341|required from 'void boost::archive::detail::pointer_iserializer::load_object_ptr(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::text_iarchive; T = Player]'|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|76|required from here|
>C:\Development\Libraries\boost\boost\serialization\access.hpp|132|error: no matching function for call to 'Player::Player()'|
>C:\Development\Libraries\boost\boost\serialization\access.hpp|132|note: candidates are:|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|8|note: Player::Player(int, int)|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|8|note: candidate expects 2 arguments, 0 provided|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|3|note: Player::Player(bool)|
>C:\Development\Projects\Roguelike\Roguelike\player.cpp|3|note: candidate expects 1 argument, 0 provided|
>C:\Development\Projects\Roguelike\Roguelike\player.h|12|note: Player::Player(const Player&)|
>C:\Development\Projects\Roguelike\Roguelike\player.h|12|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 1 error(s), 5 warning(s) (0 minute(s), 4 second(s)) ===|
Thanks very much. If any other information is required, I will append it.
The error message is pretty clear:
no matching function for call to 'Player::Player()'
You must have a default constructor in your class.
In addition to what Joachim said, in some cases you will not be able to make a type default-constructible (especially when you're adding non-intrusive serialization to types from a 3rdparty library).
In that case learn about the load_construct_data customization point:
Non-default Constructors
This mechanism is already automatically employed for you e.g. in the deserialization of STL containers whose element type has no default constructor.