0

I have this interface :

class ISocketClient
{
public:
  ~ISocketClient() {}
  virtual bool      connectToHost(std::string const &hostname, unsigned short port) = 0;
  virtual void      closeClient() = 0;
  virtual bool      sendMessage(Message &) = 0;
  virtual Message   *getMessage() = 0;
};

And this is my class that inherits is:

class TCPClient : public  QObject, ISocketClient
{
  Q_OBJECT

public:
  TCPClient(QObject *parent = 0);
  ~TCPClient();
  bool connectToHost(std::string const &hostname, unsigned short port);
  void closeClient();
  bool sendMessage(Message &);
  Message *getMessage();
public slots:
  void readMessage();
private:
  QTcpSocket *tcpSocket;
};

But when I compile I have this error:

/home/danilo_d/Epitech-Projects/Semestre5/QtNetworkTest/TCPClient.cpp:4: error: undefined reference to `vtable for TCPClient'

And when I take out my inheritance of QObject, it works.

Have any idea how I can do that ?

Dimitri Danilov
  • 1,338
  • 1
  • 17
  • 45
  • 1
    Can you show the `TCPClient.cpp`? – Adrian Colomitchi Oct 15 '16 at 12:05
  • I think you want: `class TCPClient : public QObject, public ISocketClient`, anyway `~ISocketClient() {}` should be virtual – Danh Oct 15 '16 at 12:14
  • @user3528438 Really? That's not how I read the [Multiple Inheritance Requires QObject to Be First](http://doc.qt.io/qt-4.8/moc.html#multiple-inheritance-requires-qobject-to-be-first) requirement. – Adrian Colomitchi Oct 15 '16 at 12:15
  • @user3528438 as long as `ISocketClient` didn't inherit from `QObject`, and QObject is the first base, it's fine – Danh Oct 15 '16 at 12:15

1 Answers1

1

That's probably because you aren't including the .moc file in the build. See Qt Linker Error: "undefined reference to vtable" for a similar problem, although, provided that your build system is unknown, the idea is that:

a) you need to make sure moc is ran on your .cpp file and

b) that the resulting .cpp file is included in the build.

How exactly it is done, depends on the build system. For instance, in my current project, with Cmake 3.x.x, this command is enough:

set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)

For GNU make, here is an example of how it can be done:

http://doc.qt.io/qt-4.8/moc.html#writing-make-rules-for-invoking-moc

For qmake, see e.g.

Why is important to include ".moc" file at end of a Qt Source code file?

As for multiple inheritance, that's not allowed if multiple QObject's are to be inherited. On the contrary, when there is one QObject and multiple "conventional" classes, that works fine.

Community
  • 1
  • 1
iksemyonov
  • 4,106
  • 1
  • 22
  • 42