简体   繁体   English

声明函数指针C ++ arduino 1.6.11

[英]Declaration function pointer C++ arduino 1.6.11

I am a beginner in C++, but for my arduino I need this code. 我是C ++的初学者,但对于我的arduino,我需要此代码。 It is compiling and working for a lower version of arduino, eg arduino-1.6.5.r5. 它正在为较低版本的arduino进行编译和工作,例如arduino-1.6.5.r5。 From 1.6.9 onwards and even the latest version of arduino (1.6.11) this declaration is not compiling anymore. 从1.6.9起,甚至是最新版本的arduino(1.6.11),此声明都不再编译。 I do not understand the error completely. 我不完全理解该错误。 Can it be rewritten in another way? 可以用其他方式重写吗? I am using a library CallBack.h. 我正在使用图书馆CallBack.h。

source of the library: https://bitbucket.org/ehsmaes/cmdcallback/wiki/Home 库的来源: https : //bitbucket.org/ehsmaes/cmdcallback/wiki/Home

Example from library ` 库中的示例

#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

This error means that function baseevent wasn't declared BEFORE CallBackDef f[] = ... . 此错误意味着在CallBackDef f[] = ...之前未声明函数baseevent You need function prototype before it's used, or function definition (= complete function before it's used). 您需要在使用函数原型之前或函数定义(在使用函数之前=完整函数)。

There is also another preprocessor in the Arduino, that takes care of these definitions and it puts them somewhere around sketch beggining. Arduino中还有另一个预处理器,负责处理这些定义,并将它们放置在草图开始处。 But anything more complex is usually destroyed (generated prototypes are completely wrong), so even legal c++ code can't be compiled. 但是通常会破坏任何更复杂的东西(生成的原型是完全错误的),因此即使合法的c ++代码也无法编译。 And it sometimes change it's behavior between Arduino versions. 有时它会改变Arduino版本之间的行为。

Example CmdCallBack_example_minimum is not working by default in 1.6.9 but, if you add function prototype: 示例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));
}

Without explicit function prototype Arduino preprocessor takes care of it, but it gets inserted AFTER the line where it is used. 没有显式函数原型的Arduino预处理器会处理它,但是它会在使用它的行之后插入。 So there is still error. 因此仍然存在错误。

Your version however is broken even after that fix (some incorrect parameter type in CallBack cmd...) 但是,即使在修复后,您的版本也会被破坏(CallBack cmd中的某些错误参数类型...)

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

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