简体   繁体   中英

Declaration function pointer C++ arduino 1.6.11

I am a beginner in C++, but for my arduino I need this code. It is compiling and working for a lower version of arduino, eg 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. I do not understand the error completely. Can it be rewritten in another way? I am using a library CallBack.h.

source of the library: 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[] = ... . 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. But anything more complex is usually destroyed (generated prototypes are completely wrong), so even legal c++ code can't be compiled. And it sometimes change it's behavior between Arduino versions.

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

Without explicit function prototype Arduino preprocessor takes care of it, but it gets inserted AFTER the line where it is used. So there is still error.

Your version however is broken even after that fix (some incorrect parameter type in CallBack cmd...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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