簡體   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