声明函数指针c++ arduino 1.6.11
Declaration function pointer C++ arduino 1.6.11
我是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…)
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 人脸跟踪arduino代码的优化
- vscode下的Arduino代码出现意外编译错误
- Arduino从Txt读取整数
- Arduino C++在构造函数中用参数声明对象数组
- C++ Boost::asio串行通信与Arduino无法写入
- Arduino millis() - millis() 怎么能等于 0 以外的任何东西?
- 在Arduino中将PWM通道命名为LEDC是否有特定原因
- 'val' Arduino 错误代码之前的预期'('
- 我可以在 Arduino 的库文件夹之外安装库吗?
- 在 Arduino 上使用 sscanf 会导致与 const char * 不匹配,并且返回值始终相同,尽管输入值不同
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- Arduino-C++ bool 不会从 false 变为 true
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- cmsis_compiler.h:在为 Arduino Due 编译 Arduino Tensorflow lite 库
- Arduino SocketIoClient - 没有用于调用的匹配函数
- Arduino无法编译名为SP:"Expected ')' before '(' token"的变量
- 当我尝试通过 arduino 上的引用传递布尔数组时出现问题