Arduino/c++ 在另一个 class 中使用 SoftwareSerial class
Arduino/c++ using SoftwareSerial class in another class
我正在尝试构建一些 class,自从我制作 OOP 以来已经有很长时间了,而且大部分时间我都忘记了一切 ;)
我正在尝试在我从 SoftwareSerial 读取的内容中构建 class,稍后再用它做点什么。
我制作了这个代码:
#include <SoftwareSerial.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
class pH
{
int Stable;
unsigned long previousMillis;
long Gap;
//SoftwareSerial pHrs(100,100);
//SoftwareSerial portOne(100, 100);
//SoftwareSerial pHrs = new SoftwareSerial(90,90);
SoftwareSerial SerialPort;
// Constructor - creates a pH
// and initializes the member variables and state
public: pH(uint8_t tx, uint8_t rx, long pHgap)
{
Gap = pHgap;
//SerialPort(rx, tx);
}
};
但是得到这个:
/home/san/Projekty/pHmetr/rs8/rs8.ino: In constructor 'pH::pH(uint8_t, uint8_t, long int)':
rs8:26: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
{
^
/home/san/Projekty/pHmetr/rs8/rs8.ino:26:3: note: candidates are:
In file included from /home/san/Projekty/pHmetr/rs8/rs8.ino:1:0:
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:89:3: note: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
^
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:89:3: note: candidate expects 3 arguments, 0 provided
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:47:7: note: constexpr SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
class SoftwareSerial : public Stream
^
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:47:7: note: candidate expects 1 argument, 0 provided
exit status 1
no matching function for call to 'SoftwareSerial::SoftwareSerial()'
我试图自己寻找解决方案,但不知何故我迷路了...
最佳注册!
您应该通过初始化列表调用 SoftwareSerial
的构造函数。
classpH
的构造函数应该是这样的:
public: pH(uint8_t tx, uint8_t rx, long pHgap)
: SerialPort(rx, tx) // this is initializer list
{
Gap = pHgap;
}
class pH
{
int Stable;
unsigned long previousMillis;
long Gap;
//SoftwareSerial pHrs(100,100);
//SoftwareSerial portOne(100, 100);
//SoftwareSerial pHrs = new SoftwareSerial(90,90);
SoftwareSerial SerialPort;
// Constructor - creates a pH
// and initializes the member variables and state
public:
pH(uint8_t tx, uint8_t rx, long pHgap)
: SoftwareSerial(/* You should initalize your base class */) // <---
{
Gap = pHgap;
//SerialPort(rx, tx);
}
};
您应该在您的构造函数中初始化您的基础 class。
两个建议都对。
public: pH(uint8_t tx, uint8_t rx, long pHgap)
: SerialPort(rx, tx) // this is initializer list
{
Gap = pHgap;
}
是我needed.Thank你
我正在尝试构建一些 class,自从我制作 OOP 以来已经有很长时间了,而且大部分时间我都忘记了一切 ;)
我正在尝试在我从 SoftwareSerial 读取的内容中构建 class,稍后再用它做点什么。
我制作了这个代码:
#include <SoftwareSerial.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
class pH
{
int Stable;
unsigned long previousMillis;
long Gap;
//SoftwareSerial pHrs(100,100);
//SoftwareSerial portOne(100, 100);
//SoftwareSerial pHrs = new SoftwareSerial(90,90);
SoftwareSerial SerialPort;
// Constructor - creates a pH
// and initializes the member variables and state
public: pH(uint8_t tx, uint8_t rx, long pHgap)
{
Gap = pHgap;
//SerialPort(rx, tx);
}
};
但是得到这个:
/home/san/Projekty/pHmetr/rs8/rs8.ino: In constructor 'pH::pH(uint8_t, uint8_t, long int)':
rs8:26: error: no matching function for call to 'SoftwareSerial::SoftwareSerial()'
{
^
/home/san/Projekty/pHmetr/rs8/rs8.ino:26:3: note: candidates are:
In file included from /home/san/Projekty/pHmetr/rs8/rs8.ino:1:0:
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:89:3: note: SoftwareSerial::SoftwareSerial(uint8_t, uint8_t, bool)
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
^
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:89:3: note: candidate expects 3 arguments, 0 provided
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:47:7: note: constexpr SoftwareSerial::SoftwareSerial(const SoftwareSerial&)
class SoftwareSerial : public Stream
^
/home/san/.local/share/umake/ide/arduino/hardware/arduino/avr/libraries/SoftwareSerial/SoftwareSerial.h:47:7: note: candidate expects 1 argument, 0 provided
exit status 1
no matching function for call to 'SoftwareSerial::SoftwareSerial()'
我试图自己寻找解决方案,但不知何故我迷路了...
最佳注册!
您应该通过初始化列表调用 SoftwareSerial
的构造函数。
classpH
的构造函数应该是这样的:
public: pH(uint8_t tx, uint8_t rx, long pHgap)
: SerialPort(rx, tx) // this is initializer list
{
Gap = pHgap;
}
class pH
{
int Stable;
unsigned long previousMillis;
long Gap;
//SoftwareSerial pHrs(100,100);
//SoftwareSerial portOne(100, 100);
//SoftwareSerial pHrs = new SoftwareSerial(90,90);
SoftwareSerial SerialPort;
// Constructor - creates a pH
// and initializes the member variables and state
public:
pH(uint8_t tx, uint8_t rx, long pHgap)
: SoftwareSerial(/* You should initalize your base class */) // <---
{
Gap = pHgap;
//SerialPort(rx, tx);
}
};
您应该在您的构造函数中初始化您的基础 class。
两个建议都对。
public: pH(uint8_t tx, uint8_t rx, long pHgap)
: SerialPort(rx, tx) // this is initializer list
{
Gap = pHgap;
}
是我needed.Thank你