Home > Software design >  Class constructor defining inherited classes constructor syntax error in header file
Class constructor defining inherited classes constructor syntax error in header file

Time:10-25

I have the below code giving me a syntax error on the BindingSocket definition, my understanding was if I wanted to define an inherited classes constructor I continue the BindingSocket definition with BindingSocket(...):Socket(...);, however this gives me a standard syntax error output.

#ifndef NETWORKING_BINDINGSOCKET_HPP_
#define NETWORKING_BINDINGSOCKET_HPP_

#include <stdio.h>
#include "Socket.hpp"

namespace HDE
{
    class BindingSocket: public Socket
    {
        public:
            BindingSocket(...) : Socket(...);
    };
}

and then within my main cpp file I can write:

HDE::BindingSocket::BindingSocket(...): Socket(...)
{

Strangely enough if I add a {} at the end of the header definition for the class I get no syntax error.

CodePudding user response:

The inheritance is given by class BindingSocket: public Socket.

The : Socket(...) after the constructor calls the parent constructor and belongs to the definition and not to the declaration.

So it has to be:

namespace HDE
{
    class BindingSocket: public Socket
    {
        public:
            BindingSocket(...);
    };
}

And:

HDE::BindingSocket::BindingSocket(...): Socket(...)
{
  •  Tags:  
  • c
  • Related