'void (*)(const char*)' to 'void (*)()' [-fpermissive]

'void (*)(const char*)' to 'void (*)()' [-fpermissive]



I'm using this Arduino library to tokenize and parse commands received over a serial port but I have some strange pointer issue. I seen some other solution on stackoverflow(as suggested) but none of them helped me fix my issue.



Please let me know what I'm doing wrong with this addCommand function. Everything seems fine to me


addCommand



Call pingHandler, pass "PING" character string to function definition & that's all.


#include <SoftwareSerial.h>
#include "SerialCommand.h"

SerialCommand sCmd;

void pingHandler (const char *command)
Serial.println("PONG");


void setup()
Serial.begin(9600);
while (!Serial);

sCmd.addCommand("PING", pingHandler);


void loop()
// put your main code here, to run repeatedly:
if(Serial.available() > 0)
sCmd.readSerial();




I'm getting this error


C:UsersGK_Desk_07DocumentsArduinoArduinoSerialCommandArduinoUnityArduinoUnity.ino: In function 'void setup()':

C:UsersGK_Desk_07DocumentsArduinoArduinoSerialCommandArduinoUnityArduinoUnity.ino:13:38: warning: invalid conversion from 'void (*)(const char*)' to 'void (*)()' [-fpermissive]

sCmd.addCommand("PING", pingHandler);

^

In file included from C:UsersGK_Desk_07DocumentsArduinoArduinoSerialCommandArduinoUnityArduinoUnity.ino:2:0:

sketchSerialCommand.h:84:8: note: initializing argument 2 of 'void SerialCommand::addCommand(const char*, void (*)())'

void addCommand(const char *, void(*)()); // Add commands to processing dictionary

^




2 Answers
2



The second argument of addCommand should be a pointer to a function taking no arguments, as shown in the example.


addCommand



Try changing pingHandler to:


pingHandler


void pingHandler ()
const char *command = sCmd.next();
Serial.println("PONG");





Note that use of empty parentheses in void pingHandler() is an obsolescent feature
– David Bowling
Aug 22 '17 at 8:58


void pingHandler()





It would have been, if this was C. But it is C++.
– rustyx
Aug 22 '17 at 9:04





Hmmm. It was the C tag that brought me here; still, I didn't think that Arduino was actually C++.
– David Bowling
Aug 22 '17 at 9:10



The definition for addCommand is


addCommand


void addCommand(const char *, void(*)()); // Add commands to processing dictionary



So it expects a void(*)() (function without any arguments in C++, with any arbitrary arguments in C) but you're giving it a const char* argument


void(*)()


const char*



Change your pingHandler to


pingHandler


void pingHandler (void) {



Edit:



Since Arduino is C++ you don't need the void part inside parentheses


void


void pingHandler () {





This is tagged c++, where we normally express "takes no arguments" with (), unlike c, where we must write (void). The latter is not idiomatic in C++.
– Toby Speight
Aug 22 '17 at 9:17



()


(void)





@TobySpeight it was tagged C at first
– phuclv
Aug 22 '17 at 10:09






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ