简体   繁体   English

'void(*)(const char *)'到'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. 我正在使用 Arduino库对通过串行端口接收的命令进行标记化和解析,但是我遇到了一些奇怪的指针问题。 I seen some other solution on stackoverflow(as suggested) but none of them helped me fix my issue. 我在stackoverflow上看到了其他解决方案(如建议的那样),但是它们都没有帮助我解决问题。

Please let me know what I'm doing wrong with this addCommand function. 请让我知道此addCommand函数在做什么。 Everything seems fine to me 一切对我来说似乎很好

Call pingHandler, pass "PING" character string to function definition & that's all. 调用pingHandler,将“ PING”字符串传递给函数定义,仅此而已。

#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:\Users\GK_Desk_07\Documents\Arduino\ArduinoSerialCommand\ArduinoUnity\ArduinoUnity.ino: In function 'void setup()':

C:\Users\GK_Desk_07\Documents\Arduino\ArduinoSerialCommand\ArduinoUnity\ArduinoUnity.ino:13:38: warning: invalid conversion from 'void (*)(const char*)' to 'void (*)()' [-fpermissive]

   sCmd.addCommand("PING", pingHandler);

                                      ^

In file included from C:\Users\GK_Desk_07\Documents\Arduino\ArduinoSerialCommand\ArduinoUnity\ArduinoUnity.ino:2:0:

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

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

        ^

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(*)() (在C ++中没有任何参数,在C中具有任何任意参数的函数),但是您要给它一个const char*参数

Change your pingHandler to 将您的pingHandler更改为

void pingHandler (void) {

Edit: 编辑:

Since Arduino is C++ you don't need the void part inside parentheses 由于Arduino是C ++,因此不需要括号内的void部分

void pingHandler () {

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM