Vote count:
0
I try to extend the Arduino IDE for ARM based boards with 9bit support for the Serial connections, but i can't get the function overloading right. I have read as many post here on SO as well as on the internet on overloading, overriding and hiding but everything i try fails with compiler errors. Hopefully anybody can explain what i need to do to achieve my goal.
Note: The Classes below show only the interesting parts.
It all begins in the Print.h:
class Print
{
public:
virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if (str == NULL) return 0;
return write((const uint8_t *)str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *)buffer, size);
}
}
Then Stream inherits from Print (but no changes to write).
In HardwareSerial.h HardwareSerial inherits from Stream:
class HardwareSerial : public Stream
{
public:
virtual size_t write(uint8_t) = 0;
using Print::write; // pull in write(str) and write(buf, size) from Print
}
And finally in USARTClass.h inherits USARTClass from HardwareSerial:
class USARTClass : public HardwareSerial
{
public:
size_t write( const uint8_t c ) ;
}
In USARTClass.cpp the write method is implemented. I want to override the write function with one that takes a uint16_t as argument:
class USARTClass : public HardwareSerial
{
public:
size_t write( const uint8_t c ) ;
size_t write( const uint16_t c ) ;
}
If i do so i always get a compiler error:
error: call of overloaded 'write(int)' is ambiguous
note: candidates are:
USARTClass.h:89:12: note: virtual size_t USARTClass::write(uint8_t)
size_t write( uint8_t ) ;
^
USARTClass.h:90:12: note: size_t USARTClass::write(uint16_t)
size_t write( uint16_t );
^
Print.h:49:12: note: size_t Print::write(const char*) <near match>
size_t write(const char *str) {
^
Print.h:49:12: note: no known conversion for argument 1 from 'int' to 'const char*'
I simply don't understand what i have to do to overload the write method.
c++ Overload a virtual function
Aucun commentaire:
Enregistrer un commentaire