HardwareSerial

I'm stumped by the stack trace. I can't figure out what's wrong with the code. I even slept on it. I removed all the bulk of the library and left only the parts in .h and .cpp that are causing the error. Does anybody see it?

LCD.h

#ifndef LCD_h
#define LCD_h

#include "WProgram.h"

class LCD
{
public:
  LCD();
  LCD(HardwareSerial LCDHardwareSerial);
  HardwareSerial hardwareSerial;
};
#endif

LCD.cpp

#include "WProgram.h"
#include "LCD.h"

HardwareSerial hardwareSerial;

LCD::LCD()
{
  hardwareSerial = Serial;
  hardwareSerial.begin(9600);
}

LCD::LCD(HardwareSerial LCDHardwareSerial)
{
  hardwareSerial = LCDHardwareSerial;
  hardwareSerial.begin(9600);
}

Stack trace

C:\Program Files\Arduino\libraries\LCD\LCD.cpp:10: error: no matching function for call to 'HardwareSerial::HardwareSerial()'
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:50: note: candidates are: HardwareSerial::HardwareSerial(ring_buffer*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:32: note:                 HardwareSerial::HardwareSerial(const HardwareSerial&)
C:\Program Files\Arduino\libraries\LCD\LCD.cpp: In constructor 'LCD::LCD()':
C:\Program Files\Arduino\libraries\LCD\LCD.cpp:12: error: no matching function for call to 'HardwareSerial::HardwareSerial()'
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:50: note: candidates are: HardwareSerial::HardwareSerial(ring_buffer*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:32: note:                 HardwareSerial::HardwareSerial(const HardwareSerial&)
C:\Program Files\Arduino\libraries\LCD\LCD.cpp: In constructor 'LCD::LCD(HardwareSerial)':
C:\Program Files\Arduino\libraries\LCD\LCD.cpp:18: error: no matching function for call to 'HardwareSerial::HardwareSerial()'
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:50: note: candidates are: HardwareSerial::HardwareSerial(ring_buffer*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, volatile uint8_t*, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\Program Files\Arduino\hardware\arduino\cores\arduino/HardwareSerial.h:32: note:                 HardwareSerial::HardwareSerial(const HardwareSerial&)

Thanks,
Stauffski

That's not a stack trace, that's a linker compiler error.

This line:

HardwareSerial hardwareSerial;

Calls the default constructor for HardwareSerial which takes no arguments. However the HardwareSerial class has no such constructor.

stauffski:
I'm stumped by the stack trace. I can't figure out what's wrong with the code. I even slept on it. I removed all the bulk of the library and left only the parts in .h and .cpp that are causing the error. Does anybody see it?

I see several problems.

  1. You should pass the HardwareSerial argument by reference (using a & sign) to avoid copying
  2. You should declare the HardwareSerial class member as a reference, so it references back to the original global (again using a & sign)
  3. You should pass the necessary arguments to the constructor of the HardwareSerial class.
  1. You should pass the necessary arguments to the constructor of the HardwareSerial class.

No. The HardwareSerial class already creates all the valid instances of HardwareSerial, as Serial, Serial1, Serial2, and Serial3. There should be no calls to the HardwareSerial class' constructor.