The following code will cause an error.
US>Error Contents
main.cpp:5:44:error:use of deleted function 'SerialPrint::SerialPrint()'
SerialPrint_serialPrint=SerialPrint();
main.cpp
#include "main.h"
// SerialPrint_serialPrint;
void setup() {
SerialPrint_serialPrint=SerialPrint();
_serialPrint.beginPrint(DEBUG_PORT_RX, DEBUG_PORT_TX, DEBUG_PORT_BAUDRATE);
}
void loop() {
}
main.h
#pragmaonce
# include "Arduino.h"
# include "SerialPrint.h"
#define DEBUG_PORT_RX11
#define DEBUG_PORT_TX12
#define DEBUG_PORT_BAUDRATE 4800
void setup();
void loop();
SerialPrint.cpp
#include "SerialPrint.h"
# include <string.h>
/**
* US>Serial port initialization for console display
* */
voidSerialPrint::beginPrint(uint8_trx,uint8_ttx,longbaudrate){
_serial=SoftwareSerial(rx,tx);
_serial.begin(baudrate);
}
voidSerialPrint::print(const char*message){
_serial.print(message);
}
voidSerialPrint::print(String message){
_serial.print(message);
}
voidSerialPrint::println(const char*message){
_serial.println(message);
}
voidSerialPrint::println(String message){
_serial.println(message);
}
SeriapPrint.h
#pragmaonce
# include <SoftwareSerial.h>
classSerialPrint {
public:
void beginPrint(uint8_trx, uint8_ttx, long border);
// static void cmpStr(const char*str1, const char*str2, char*buf);
void print(const char*message);
void println(const char*message);
void print (String message);
void println(String message);
private:
SoftwareSerial_serial;
};
setup()
function in main.cppSerialPrint_serialPrint=SerialPrint();
In , you are generating an instance of the SerialPrint
class.All member variables in the SerialPrint
class are also initialized.In this case, SoftwareSerial_serial;
is also initialized. We did not create a default constructor for SerialPrint()
, so all members are initialized with the default constructor.
The Arduino documentation suggests that the SoftwareSerial
class does not have a default constructor.As a result, SoftwareSerial_serial;
cannot be initialized, resulting in an error.
In fact, the SerialPrint::beginPrint()
method passes arguments to the SoftwareSerial
constructor, so you can do this with the SoftwareSerial
.
© 2024 OneMinuteCode. All rights reserved.