How can a member of class of type BufferedSerial be initialized?

10 Jul 2017

Hello,

I am trying to move some pieces of my code to a class named MyClass where I have a private member BufferedSerial _mySerial. Unfortunately, I got a lot of compiler's errors that I don't know how to resolve. I will appreciate any help that anyone can provide.

MyClass.h

#include "mbed.h"
#include "BufferedSerial.h"

class MyClass {
public:
    // define which pins are used
    MyClass(PinName tx, PinName rx);

private:
    BufferedSerial _MySerial;

    void _RxCallback();
};

MyClass.cpp

#include "MyClass.h"

MyClass::MyClass(PinName tx, PinName rx) : _MySerial(tx, rx, 512) {
    _MySerial.baud(230400);
    _MySerial.attach(callback(this, &MyClass::_RxCallback), Serial::RxIrq);
}

Compiler's errors

Building project stm32l476_mbed (NUCLEO_L476RG, GCC_ARM)
Scan: .
Scan: mbed
Scan: env
Scan: FEATURE_COMMON_PAL
Compile [ 73.9%]: stm32l4xx_hal_msp_template.c
Compile [ 74.2%]: stm32l4xx_hal_iwdg.c
Compile [ 74.5%]: stm32l4xx_hal_i2c_ex.c
Compile [ 74.8%]: stm32l4xx_hal_lcd.c
Compile [ 75.1%]: stm32l4xx_hal_lptim.c
Compile [ 75.4%]: stm32l4xx_hal_nand.c
Compile [ 75.7%]: stm32l4xx_hal_opamp.c
Compile [ 75.9%]: stm32l4xx_hal_opamp_ex.c
Compile [ 76.2%]: stm32l4xx_hal_irda.c
Compile [ 76.5%]: stm32l4xx_hal_nor.c
Compile [ 76.8%]: stm32l4xx_hal_pcd_ex.c
Compile [ 77.1%]: stm32l4xx_hal_pwr.c
Compile [ 77.4%]: stm32l4xx_hal_pcd.c
[Warning] stm32l4xx_hal_pcd.c@1418,11: comparison between signed and unsigned integer expressions [-Wsign-compare]
[Warning] stm32l4xx_hal_pcd.c@1433,13: comparison between signed and unsigned integer expressions [-Wsign-compare]
Compile [ 77.7%]: stm32l4xx_hal_pwr_ex.c
Compile [ 78.0%]: stm32l4xx_hal_rng.c
Compile [ 78.3%]: stm32l4xx_hal_rcc.c
Compile [ 78.6%]: main.cpp
[Warning] http_request.h@143,28: comparison between signed and unsigned integer expressions [-Wsign-compare]
[Warning] http_request.h@162,28: comparison between signed and unsigned integer expressions [-Wsign-compare]
[Error] NonCopyable.h@157,5: 'mbed::NonCopyable<T>::NonCopyable(const mbed::NonCopyable<T>&) [with T = mbed::SerialBase]' is private
[Error] SerialBase.h@42,7: within this context
[Error] NonCopyable.h@157,5: 'mbed::NonCopyable<T>::NonCopyable(const mbed::NonCopyable<T>&) [with T = mbed::RawSerial]' is private
[Error] RawSerial.h@53,7: within this context
[ERROR] In file included from ./main.h:13:0,
                 from ./main.cpp:1:
./mbed-http/source/http_request.h: In member function 'HttpResponse* HttpRequest::send(const void*, nsapi_size_t)':
./mbed-http/source/http_request.h:143:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (send_result != request_size) {
                            ^
./mbed-http/source/http_request.h:162:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (nparsed != recv_ret) {
                            ^
In file included from ./mbed-os/rtos/Thread.h:32:0,
                 from ./mbed-os/rtos/rtos.h:31,
                 from ./mbed-os/mbed.h:38,
                 from ./main.h:4,
                 from ./main.cpp:1:
./mbed-os/platform/NonCopyable.h: In copy constructor 'mbed::SerialBase::SerialBase(const mbed::SerialBase&)':
./mbed-os/platform/NonCopyable.h:157:5: error: 'mbed::NonCopyable<T>::NonCopyable(const mbed::NonCopyable<T>&) [with T = mbed::SerialBase]' is private
     NonCopyable(const NonCopyable&);
     ^
In file included from ./mbed-os/drivers/Serial.h:24:0,
                 from ./mbed-os/mbed.h:81,
                 from ./main.h:4,
                 from ./main.cpp:1:
./mbed-os/drivers/SerialBase.h:42:7: error: within this context
 class SerialBase : private NonCopyable<SerialBase> {
       ^
In file included from ./mbed-os/mbed.h:88:0,
                 from ./main.h:4,
                 from ./main.cpp:1:
./mbed-os/drivers/RawSerial.h: In copy constructor 'mbed::RawSerial::RawSerial(const mbed::RawSerial&)':
./mbed-os/drivers/RawSerial.h:53:7: note: synthesized method 'mbed::SerialBase::SerialBase(const mbed::SerialBase&)' first required here 
 class RawSerial: public SerialBase, private NonCopyable<RawSerial> {
       ^
In file included from ./mbed-os/rtos/Thread.h:32:0,
                 from ./mbed-os/rtos/rtos.h:31,
                 from ./mbed-os/mbed.h:38,
                 from ./main.h:4,
                 from ./main.cpp:1:
./mbed-os/platform/NonCopyable.h:157:5: error: 'mbed::NonCopyable<T>::NonCopyable(const mbed::NonCopyable<T>&) [with T = mbed::RawSerial]' is private
     NonCopyable(const NonCopyable&);
     ^
In file included from ./mbed-os/mbed.h:88:0,
                 from ./main.h:4,
                 from ./main.cpp:1:
./mbed-os/drivers/RawSerial.h:53:7: error: within this context
 class RawSerial: public SerialBase, private NonCopyable<RawSerial> {
       ^
In file included from ./MyClass.h:5:0,
                 from ./main.h:9,
                 from ./main.cpp:1:
./easy-connect/esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedSerial.h: In copy constructor 'BufferedSerial::BufferedSerial(const BufferedSerial&)':
./easy-connect/esp8266-driver/ESP8266/ATParser/BufferedSerial/BufferedSerial.h:71:7: note: synthesized method 'mbed::RawSerial::RawSerial(const mbed::RawSerial&)' first required here 
 class BufferedSerial : public RawSerial 
       ^
In file included from ./main.h:9:0,
                 from ./main.cpp:1:
./MyClass.h: In copy constructor 'MyClass::MyClass(const MyClass&)':
./MyClass.h:27:7: note: synthesized method 'BufferedSerial::BufferedSerial(const BufferedSerial&)' first required here 
 class MyClass {
       ^
./main.cpp: At global scope:
./main.cpp:7:87: note: synthesized method 'MyClass::MyClass(const MyClass&)' first required here 
 MyClass cam = MyClass(MBED_CONF_APP_CAM_TX, MBED_CONF_APP_CAM_RX, MBED_CONF_APP_CAM_RESET);
                                                                                       ^

[mbed] ERROR: "/usr/bin/python" returned error code 1.
11 Jul 2017

I have answered my own question :)

MyClass.h

#include "mbed.h"
#include "BufferedSerial.h"
 
class MyClass {
public:
    // define which pins are used
    MyClass(PinName tx, PinName rx);
 
private:
    BufferedSerial *_MySerial;
 
    void _RxCallback();
};

MyClass.cpp

#include "MyClass.h"
 
MyClass::MyClass(PinName tx, PinName rx)  {
    _MySerial = new BufferedSerial(tx, rx);
    _MySerial->baud(230400);
    _MySerial->attach(callback(this, &MyClass::_RxCallback), Serial::RxIrq);
}
25 Jul 2017

Many thanks! that way I fixed MQTTmbed.h