我是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
答案 0 :(得分:0)
此错误表示未在baseevent
之前声明函数CallBackDef f[] = ...
。在使用之前需要函数原型,或函数定义(在使用之前完成函数)。
Arduino中还有另一个预处理器,它负责这些定义并将它们放在草图开始周围。但是任何更复杂的东西通常都会被破坏(生成的原型完全错误),所以即使合法的c ++代码也无法编译。它有时会改变它在Arduino版本之间的行为。
示例CmdCallBack_example_minimum在1.6.9中默认不起作用,但是,如果添加函数原型:
#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中的某些不正确的参数类型......)
,您的版本也会被破坏