声明函数指针c++ arduino 1.6.11

Declaration function pointer C++ arduino 1.6.11

本文关键字:arduino c++ 函数 指针 声明      更新时间:2023-10-16

我是c++的初学者,但对于我的arduino我需要这个代码。它正在编译并工作于较低版本的arduino,例如arduino-1.6.5.r5。从1.6.9开始,甚至最新版本的arduino(1.6.11),这个声明不再编译。我不能完全理解这个错误。它可以用另一种方式重写吗?我正在使用一个库CallBack.h.

库的源代码:https://bitbucket.org/ehsmaes/cmdcallback/wiki/Home

库中的示例'

#include <CallBack.h>
// Compile and upload to arduino. Run the serial monitor and type command
// :help;
// Values for initiation of cmd/response interface. 
// After initial boot, id, gid and del are stored in eeprom.
// Change values by command. Make sure each device has a unique id.
String descr="Command/response test program v0.1";
String id="a1";
String gid="a";
int    del=0; //delayed response
// List of commands defined by keyword, funtion pointer, number of arguments 
// and description used in "help" command.
CallBackDef f[] = {
  {(String)"add",   (FunctionPointer)&add,  (int)2, (String)":num1:num2"}
};
// initiate command handler: function array, number of functions and intial values
CallBack cmd(f, sizeof(f) / sizeof(*f), id, gid, descr, del);
void setup() {
  Serial.begin(9600);
  cmd.ok(); // say hello
}
void loop() {
  // Put code here. Use timers instead of delay if possible as not to disrupt
  // command/response interaction with host
}
void serialEvent() {
  // Don't forget this line. Parse command if serial data is available.
  cmd.cmdCheck();
}
//   --------- command initiated callback functions below ---------
// callback functions all need to be defined void and with String argv
// argument list. The command parser will validate the number of input
// parameters but any additional validation has to be perfomed by each
// callback function. As the argument list is passed as strings, type
// casting to other types is the responsibility of the function.

void add(String argv[]) {
  int a = cmd.stoi(argv[0]);
  int b = cmd.stoi(argv[1]);
  cmd.respond(String(a + b));
}

    CmdCallBack_example_minimum:17: error: 'add' was not declared in this scope
   {(String)"add",   (FunctionPointer)&add,  (int)2, (String)":num1:num2"}
                                       ^
Using library CmdCallBack in folder: /Users/adrian/ownCloud/Arduino/libraries/CmdCallBack (legacy)
exit status 1
'add' was not declared in this scope

这个错误意味着函数baseevent没有在CallBackDef f[] = ...之前声明。在使用函数之前,你需要函数原型,或者函数定义(=在使用之前完成函数)。

在Arduino中还有另一个预处理器,它负责这些定义,并将它们放在草图开始的某个地方。但是任何更复杂的东西通常都会被销毁(生成的原型是完全错误的),所以即使是合法的c++代码也无法编译。它有时会在Arduino版本之间改变它的行为。

示例CmdCallBack_example_minimum在1.6.9中默认不工作,但是,如果你添加函数prototype:

#include <CallBack.h>
// Compile and upload to arduino. Run the serial monitor and type command
// :help;
// Values for initiation of cmd/response interface. 
// After initial boot, id, gid and del are stored in eeprom.
// Change values by command. Make sure each device has a unique id.
String descr="Command/response test program v0.1";
String id="a1";
String gid="a";
int    del=0; //delayed response
byte   echo=1; // command back to host
// ------------------------------------------------
// Function Prototype for add:
void add(String argv[]);
// List of commands defined by keyword, funtion pointer, number of arguments 
// and description used in "help" command.
CallBackDef f[] = {
  {(String)"add",   (FunctionPointer)&add,  (int)2, (String)":num1:num2"}
};
// initiate command handler: function array, number of functions and intial values
CallBack cmd(f, sizeof(f) / sizeof(*f), id, gid, descr, del, echo);
void setup() {
  Serial.begin(9600);
  cmd.ok(); // say hello
}
void loop() {
  // Don't forget this line. Parse command if serial data is available.
  cmd.cmdCheck();
  // Put code here. Use timers instead of delay if possible as not to disrupt
  // command/response interaction with host

}
//   --------- command initiated callback functions below ---------
// callback functions all need to be defined void and with String argv
// argument list. The command parser will validate the number of input
// parameters but any additional validation has to be perfomed by each
// callback function. As the argument list is passed as strings, type
// casting to other types is the responsibility of the function.
void add(String argv[]) {
  int a = cmd.stoi(argv[0]);
  int b = cmd.stoi(argv[1]);
  cmd.respond(String(a + b));
}

没有明确的函数原型Arduino预处理器照顾它,但它被插入到使用它的行之后。所以还是有错误

你的版本然而是坏的,即使在修复(一些不正确的参数类型在CallBack cmd…)