繁体   English   中英

声明函数指针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

此错误意味着在CallBackDef f[] = ...之前未声明函数baseevent 您需要在使用函数原型之前或函数定义(在使用函数之前=完整函数)。

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中的某些错误参数类型...)

暂无
暂无

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

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