CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2023
    Posts
    1

    Constructor for derived class

    Hello,
    I am adding some methods to the NTPClient by creating a derived class MyNTP.
    But now, I can't figure out how to invoke the new class. That is, I can't figure out how to write the constructor for this.
    NTPClient.h contains a bunch of constructors...
    Code:
      NTPClient(UDP &udp);
      NTPClient(UDP &udp, long timeOffset);
      NTPClient(UDP &udp, const char *poolServerName);
      NTPClient(UDP &udp, const char *poolServerName, long timeOffset);
      NTPClient(UDP &udp, const char *poolServerName, long timeOffset, unsigned long updateInterval);
      NTPClient(UDP &udp, IPAddress poolServerIP);
      NTPClient(UDP &udp, IPAddress poolServerIP, long timeOffset);
      NTPClient(UDP &udp, IPAddress poolServerIP, long timeOffset, unsigned long updateInterval);
    But lets just focus on the first, and simplist of these constructors.
    In NTPClient.cpp it is defined as:
    Code:
    NTPClient::NTPClient(UDP& udp) {
      this->_udp            = &udp;
    }
    So, I create MyNPT.cpp with the following constructor:
    Code:
    #include "Arduino.h"
    #include "NTPClient.h"
    #include "MYNPT.h"
    
    void MyNPT(UDP& udp)
            : NTPClient(udp)
        {
        }
    and MyNPT.h as follows
    Code:
    #ifndef _MY_NTP_H_
    #define _MY_NTP_H_
    #include "Arduino.h"
    
    #include "NTPClient.h"
    #include <Udp.h>
    
    class MyNTP : public NTPClient
    {
    public:
      void MyNPT(UDP& udp); 
    };
    
    #endif //_MY_NTP_H_
    and I get the following errors...
    1: MyNPT.h " Function definition for 'MyNPT' not found. "
    2: MyNPT.cpp At the ":" " expected a '{' "

    What am I missing?

  2. #2
    Join Date
    Nov 2018
    Posts
    116

    Re: Constructor for derived class

    > MyNPT.cpp
    The function isn't a member of the class.

    Code:
    void MyNTP::MyNPT(UDP& udp)
            : NTPClient(udp)
        {
        }

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,821

    Re: Constructor for derived class

    Is this a class exercise? See https://cplusplus.com/forum/general/285683/ where this question has been asked and answered previously.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2018
    Posts
    116

    Re: Constructor for derived class

    I thought it looked familiar!
    This just became 90% a repost bot where this is just bait for some spammy links.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured