0

I've a set of possible connections in my SW so I decided to use the Factory Pattern, so I created the base class (both .h and .cpp)

The following the content of Connection.h header file

namespace Connection
{
    class Connection
    {
        public: 
             Connection();
             ~Connection();

             virtual void sendPacket(Packet* p) = 0;
             virtual void receivePacket() = 0;
             virtual int connect() = 0;
             virtual void disconnect() = 0; 
     }
}

Even a Connection.cpp file exists but it has only an empty constructor and deconstructor.

Next, I created the derivated class (both .h and .cpp files)

namespace Connection
{
    class SocketConnection : public Connection
    {
        public:
            SocketConnection();
            ~SocketConnection();
    }
}

and its relative SocketConnection.cpp file where I'm trying to define the pure virtual methods defined in Connection.h

namespace Connection
{
    SocketConnection::SocketConnection() { }
    SocketConnection::~SocketConnection() { }

    int connect()
    {
        //Design of socket connection
    }
}

Next, I created a new Connection, DatabaseConnection defined pratically in the same way of the SocketConnection().

The error I get is

multiple definition of Connection::connect();

while invoking the Cross G++ Linker but I can't find out the reason. Can anyone tell me where I'm wrong? Thank you!

nvoigt
  • 75,013
  • 26
  • 93
  • 142
user2896152
  • 762
  • 1
  • 9
  • 32
  • You need to implement all of the pure virtual functions in the derived classes of the interface. Read [this](https://stackoverflow.com/questions/1306778/c-virtual-pure-virtual-explained). – Incomputable Oct 27 '17 at 13:45
  • 1
    First of all you have to declare virtual function you override in derived class, second that function has to belong to that class, you define function in namespace, not method. – Slava Oct 27 '17 at 13:47
  • 1
    Please don't have a namespace that contains a class with the same name. – Caleth Oct 27 '17 at 13:56

2 Answers2

4

If you want to override connect in SocketConnection then you have to declare it in the class definition:

namespace Connection
{
    class SocketConnection : public Connection
    {
        public:
            SocketConnection();
            ~SocketConnection();
             virtual int connect(); // virtual is not required here but it doesn't hurt
    }
}

Then the definition has to be properly scoped:

namespace Connection
{
    SocketConnection::SocketConnection() { }
    SocketConnection::~SocketConnection() { }

    int SocketConnection::connect()
    {
        //Design of socket connection
    }
}
Tomek
  • 4,554
  • 1
  • 19
  • 19
0

Inside your SocketConnection.h you should add:

virtual int connect();

And inside your SocketConnection.cpp you should define a scoped SocketConnection::connect() instead of connect(), like this:

int SocketConnection::connect()
{
    //Design of socket connection
}

Same for all other connection types.

Daniel Trugman
  • 8,186
  • 20
  • 41