Dimitri Danilov
Dimitri Danilov

Reputation: 1352

How can my class inherit an interface and QObject ?

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 ?

Upvotes: 0

Views: 891

Answers (1)

iksemyonov
iksemyonov

Reputation: 4196

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.

Upvotes: 1

Related Questions