0

I'm trying to adapt Arduino's HardwareSerial and Print libraries so they print to my UDP. myudpSerial.print() and .println() work nicely, but calling myupdSerial.write(c) doesn't. Relevant code in Print.h:

class Print:
{
    virtual ~Print() {}
    virtual size_t write(uint8_t) = 0; // I suspect I have to do something with this
    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);
    }
}

My own class looks like this:

class UdpSerial: public HardwareSerial{
    using HardwareSerial::HardwareSerial;
    public: 
    size_t write(const uint8_t *buffer, size_t size) override;
    //size_t write(uint8_t) override = 0;  // I suspect I have to override this
};

That works because the Print class has overloads that all eventually use the write() function. But when I call myupdSerial.write('B'); it tells me
error: no matching fucntion call to 'myudpSerial::write(char&)'
and it suggests size_t write(const uint8_t *buffer, size_t size) override; as a candidate.

As I commented, I suspect i have to override size_t write(uint8_t) = 0, but the =0 at the end confuses me and I don't know how to do it, nor what that construction is called so I could google it.

Suggestions are welcome.

Sopwafel
  • 43
  • 8
  • Any function that ends in `= 0;` is pure virtual, and therefore your class must implement an override to that function if you want to instantiate an instance of your class. – Cory Kramer Apr 06 '20 at 13:24
  • _" I don't know what that construction is called so I could google it."_ You're looking for [**pure virtual**](https://stackoverflow.com/questions/1306778). – Drew Dormann Apr 06 '20 at 13:27
  • Note that its not `virtual type function(type) = value;`. It's `virtual type function(type) = 0;`. No other value is allowed. Yes, it's a hack. – Pete Becker Apr 06 '20 at 14:13
  • Must be exactly `0` verbatim. Can't be `false` or `0U` or `0ULL` or `int{}` or `(1 -1)`. – Eljay Apr 06 '20 at 17:05

1 Answers1

0

Apparently I had to copy a bunch of overloads for write() from HardwareSerial because they were inline. That explains why they were working for Serial, and not for myupdSerial.
So: inline overloads aren't inherited. Makes sense now that I read up on them.

Sopwafel
  • 43
  • 8
  • add `using Print::write;` https://github.com/jandrassy/StreamLib/blob/9472c1618fce4552591d2caba080c08e1446cc79/src/BufferedPrint.h#L37 – Juraj Apr 06 '20 at 13:46