gpt4 book ai didi

c++ - 编译错误Arduino C++对象库

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:42 24 4
gpt4 key购买 nike

我正在为蓝牙通信编写一个 C++ Arduino 库。我实现了一种机制,以便注册一个自定义函数,以便在某个字节来自 BT 连接时执行。实际上是一种 Java Hashmap。一切正常,但是当我尝试在构造函数中注册库对象的方法时,出现了一些我无法理解的编译错误。我在 Ubuntu 16.04 LTS 64 位上使用 Arduino IDE 1.6.9。该板是一个 Arduino UNO。在代码下方, header :

#ifndef BlueHartwin_H
#define BlueHartwin_H

#include <SoftwareSerial.h>
#include <Streaming.h>;
#include <Arduino.h>

#define START_TX 254
#define STOP_TX 255

typedef void (* CmdFuncPtr) (); // this is a typedef to command functions


class BlueHartwin {

public:
BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate);
~BlueHartwin();
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr);
boolean runCmd(byte id);
void setDataLength(byte dataLength);
byte getDataLength();
byte getIncomingByte();
void txSwitchOff(void);
void txSwitchOn(void);
bool isTx();
boolean available();
boolean read();
void write(byte data,boolean endLine);


private:

CmdFuncPtr setOfCmds[255];
byte mDataLength;
bool tx;
byte incomingByte;
};

#endif

正文:

#include "BlueHartwin.h";

/*
* Reserved command IDs:
* 14,
* 15
*/


SoftwareSerial bluetooth(7,8);


BlueHartwin::BlueHartwin(byte bluetoothTx,byte bluetoothRx,long baudRate){
bluetooth = SoftwareSerial(bluetoothTx,bluetoothRx);
bluetooth.begin(baudRate);
for (int i=0;i<=255;i++) {
setOfCmds[i]=NULL;
}
tx=false;

registerCmd(14,&txSwitchOff);
registerCmd(15,&txSwitchOn);
}

BlueHartwin::~BlueHartwin(){

}

boolean BlueHartwin::registerCmd(byte id,CmdFuncPtr cmdFuncPtr){
if (setOfCmds[id]==NULL) {
setOfCmds[id]=cmdFuncPtr;
return true;
} else return false;
}

boolean BlueHartwin::unRegisterCmd(byte id,CmdFuncPtr cmdFuncPtr){
setOfCmds[id]=NULL;
return true;

}


boolean BlueHartwin::runCmd(byte id){
if (setOfCmds[id]!=NULL) {
setOfCmds[id]();
return true;
} else return false;
}


void BlueHartwin::setDataLength(byte dataLength) {
mDataLength=dataLength;
}

byte BlueHartwin::getDataLength(){
return mDataLength;
}

byte BlueHartwin::getIncomingByte(){
return incomingByte;
}

boolean BlueHartwin::isTx(){
return tx;
}

void BlueHartwin::txSwitchOff(void){
tx=false;
Serial<<"now tx is "<<tx<<endl;
}

void BlueHartwin::txSwitchOn(void){
tx=true;
Serial<<"now tx is "<<tx<<endl;
}

boolean BlueHartwin::available(){
return bluetooth.available();
}

boolean BlueHartwin::read(){

if (bluetooth.available()>0)
{
incomingByte=bluetooth.read();
Serial << "Incoming byte " << incomingByte<<endl;
return runCmd(incomingByte);
}
return false;
}


void BlueHartwin::write(byte data,boolean endLine){
if (tx) {
if (endLine) bluetooth << data << endl;
else bluetooth<<data;
}
}

错误:

/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BlueHartwin::txSwitchOff' [-fpermissive]
registerCmd(14,&txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(14,&txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:29: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:18: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say '&BlueHartwin::txSwitchOn' [-fpermissive]
registerCmd(15,&txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(15,&txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:28: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.

如果我在构造函数中更改行:

registerCmd(14,&txSwitchOff);
registerCmd(15,&txSwitchOn);

registerCmd(14,&BlueHartwin::txSwitchOff);
registerCmd(15,&BlueHartwin::txSwitchOn);

我遇到了这个错误:

/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp: In constructor 'BlueHartwin::BlueHartwin(byte, byte, long int)':
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(14,&BlueHartwin::txSwitchOff);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:21:42: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: error: no matching function for call to 'BlueHartwin::registerCmd(int, void (BlueHartwin::*)())'
registerCmd(15,&BlueHartwin::txSwitchOn);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:22:41: note: candidate is:
In file included from /home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.cpp:1:0:
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: boolean BlueHartwin::registerCmd(byte, CmdFuncPtr)
boolean registerCmd(byte id,CmdFuncPtr cmdFuncPtr);
^
/home/paolo/ARDUINO/arduino-1.6.9/libraries/BlueHartwin/BlueHartwin.h:19:10: note: no known conversion for argument 2 from 'void (BlueHartwin::*)()' to 'CmdFuncPtr {aka void (*)()}'
exit status 1
Errore durante la compilazione per la scheda Arduino/Genuino Uno.

在此先感谢您的帮助!

最佳答案

伙计,你的 txSwitchOn/Off 方法是实例方法 - 它们需要 this 才能正常工作。

另一方面,您的 CmdFuncPtr 是一个不依赖于任何东西的函数的定义。


假定 Arduino 一次可以处理多少这样的设备(由您的库支持)?

如果它是单个的,则将所有内容声明为静态的,将构造函数重命名为“static void init()”,将析构函数重命名为“static void disconnect()”,等等。你的函数(都是静态的)。

关于c++ - 编译错误Arduino C++对象库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39193478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com