Adding methods to class via derived class

I am using an NTPClient and want to add a few methods to its functionality. I have no difficulty going into the source of the client and, for example, adding the following to NTPClient.cpp
1
2
3
4
5
6
7
8
9
String NTPClient::getDayName() const
{
  const String thedays[] = {
      "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  // reference: int NTPClient::getDay() const {
  //            return (((this->getEpochTime()  / 86400L) + 4 ) % 7); //0 is Sunday
  //            }
  return (thedays[this->getDay()]);
}

and the following to NTPClient.h
 
String getDayName() const;


But what I want to do instead is add my own derived class, call it MyNTPHelpers, which has a series of methods that I control so, for example, when NTPClient.h and NTPClient.cpp are loaded after a clean, I don't have to go and edit them again.

Here is what I have come up with but I am missing something ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// MyNTPHelpers.cpp
#include "Arduino.h"
#include "NTPClient.h"
#include "NTP_Helpers.h"

String getDayName() //const
{
    const String thedays[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    return (thedays[this->getDay()]);
}


// MyNTPHelpers.h
#ifndef _NTP_HELPERS_H_
#define _NTP_HELPERS_H_

#include "Arduino.h"
#include "NTPClient.h"

class MyNTP : public NTPClient
{
public:
    String getDayName() const;
};

#endif // _NTP_HELPERS_H_ 

I see two issues in the .cpp file which I do not understand how to fix:
1: The "const" qualifier is not allowed "a type qualifier is not allowed on a nonmember function"
2: The "this->" is also not allowed. "'this' may only be used inside a nonstatic member function"

In my limited brain, these two issues are sort of working against each other. If I could have the "const" qualifier, than the member function would be static (and we see that in the first example above where I include the code directly in the NTPClient instead of trying to create a derived class (or am I totally confused, as usual)?

Any help to straighten me out on this is greatly appreciated.
solved... but only in part...

added "MyNTP::" in .cpp

1
2
3
4
5
6
String MyNTP::getDayName() const
{
    const String thedays[] = {
        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    return (thedays[this->getDay()]);
}


NOW... instead of invoking the NTPClient this way
1
2
#include <NTPClient.h>
NTPClient timeClient(ntpUDP);

I think I should invoke NTPClient through my derived class, MyNPT, like this...
1
2
#include "NTP_Helpers.h"
MyNPT timeClient(ntpUDP);


but then I get the error: "MyNPT' does not name a type" So I think I need to add a constructor of some sort to NTP_Helpers. Is that correct... and what form would that take?

why not just this in NTP_Helpers.h
1
2
3
4
 MyNPT(UDP& udp)
      : NTPClient(udp)
 {
 }

does not work... nor anything else I have tried so far...
Last edited on
Topic archived. No new replies allowed.