title: Arduino: overriding Print class problems
body: <p>I am trying to make a library for redirection of data printed to Print class. I am unfortunately stuck on error that reads</p>
  
  <blockquote>
    <p>error: cannot declare variable 'diagData' to be of abstract type 'PrintToString'</p>
    
    <p>note:   because the following virtual functions are pure within 'PrintToString'</p>
    
    <p>note:     virtual size_t PrintToString::write(uint8_t)</p>
  </blockquote>
  
  <p>I tried several variations of how to implement this but with no luck. (Sourced from the internet)</p>
  
  <hr>
  
  <p><strong>Links</strong></p>
  
  <p>Print class: github.com/ <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Print.h" rel="nofollow noreferrer">Print.h</a> and <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Print.cpp" rel="nofollow noreferrer">Print.cpp</a></p>
  
  <p><strong>My code</strong></p>
  
  <p>PrintToString.h</p>
  
  <pre><code>#ifndef PRINT_TO_STRING_H
  
      #define PRINT_TO_STRING_H
  
      #include &lt;Arduino.h&gt;
  
  
      class PrintToString : public Print
      {
          private:
              String* data;
  
          public:
              PrintToString();
              ~PrintToString();
  
              String* results();
              void clear();
  
              size_t write(uint8_t) = 0;
              size_t write(const uint8_t* buffer, size_t size);
      };
  
  #endif
  </code></pre>
  
  <p>PrintToString.cpp</p>
  
  <pre><code>#include "PrintToString.h"
  
  
  
  PrintToString::PrintToString()
  {
      data = new String();
  }
  
  PrintToString::~PrintToString()
  {
      delete data;
      data = NULL;
  }
  
  
  String* PrintToString::results()
  {
      return data;
  }
  
  void PrintToString::clear()
  {
      delete data;
      data = new String();
  }
  
  
  size_t PrintToString::write(const uint8_t* buffer, size_t size)
  {
      size_t n = 0;
  
      while (size--)
      {
          if (data-&gt;concat(*buffer++))
              n++;
          else
              break;
      }
  
      return n;
  }
  </code></pre>
  
  <p>TestSketch.ino (I have left out content of all the constants)</p>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;PrintToString.h&gt;
  
  const char*    WIFI_SSID
  const char*    WIFI_PASS
  
  const char*    API_HOST
  const uint16_t API_PORT
  const uint16_t LOCAL_UDP_PORT
  
  WiFiUDP UDPClint;
  PrintToString diagData;
  uint64_t packetNumber = 0;
  
  void setup()
  {
      WiFi.begin(WIFI_SSID, WIFI_PASS);
      UDPClint.begin(LOCAL_UDP_PORT);
  
      while (WiFi.status() != WL_CONNECTED)
          delay(500);
  
      WiFi.printDiag(diagData);
      sendStringPacket(diagData.result());
      diagData.clear();
  }
  
  void loop()
  {
      delay(1000);
  }
  
  void sendStringPacket(String payload)
  {
      UDPClint.beginPacket(API_HOST, API_PORT);
  
  
      uint64_t thisPNumber = packetNumber++;
      String thisPNumberStr;
  
      while (thisPNumber &gt; 0)
      {
          uint8_t digit = thisPNumber % 10;
          thisPNumberStr.concat(digit);
  
          thisPNumber /= 10;
      }
  
      UDPClint.write(';');
  
      for (uint64_t i = 0; i &lt; payload.length(); i++)
          UDPClint.write(payload.charAt(i));
  
      UDPClint.endPacket();
  }
  </code></pre>