How can my class inherit an interface and QObject ?

Dimitri Danilov

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 ?

iksemyonov

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I inherit from Rational (or any class with no constructor)?

From Dev

How can I create a subclass that inherit Ellipse class

From Dev

How can my subclass properly inherit a static variable

From Dev

Why interface cannot inherit pure abstract class

From Dev

How can we inherit UITableViewDataSource protocol in our class?

From Dev

How can I know what Classes inherit from a Class or Interface?

From Dev

How can I tell if my interface{} is a pointer?

From Dev

Since Interface cannot inherit Class, then how "Object" class methods available to the interface reference in java?

From Dev

How to inherit variables to class

From Dev

How to inherit from GObject class?

From Dev

Can and how service class inherit from plain class?

From Dev

Can a class inherit the properies of a namedtuple?

From Dev

How can a class inherit from a parameterized version of itself?

From Dev

Can not inherit from final class

From Dev

Can a C++ interface inherit from a class with full implementation?

From Dev

How can I make a class both implement an Interface and inherit from another class

From Dev

Why can not inherit from Interface

From Dev

How can I create a subclass that inherit Ellipse class

From Dev

How can I inherit class in CSS?

From Dev

Can a class inherit implementation of a method contruct imposed by an interface from other class?

From Dev

QT5.3 How can I add my Qobject to the QQmlEngine and access it's Properties via QML?

From Dev

How can I define some functions individually for each interface of my class?

From Dev

Does Qt Q_PROPERTY require my class to inherit QObject?

From Dev

Can and how service class inherit from plain class?

From Dev

how to avoid interface method in my class

From Dev

How can a template class inherit from a nested template class

From Dev

How to inherit a python base class?

From Dev

How to emit signals from a class that can't inhert QObject?

From Dev

How we can inherit BaseModel class in odoo

Related Related

  1. 1

    How can I inherit from Rational (or any class with no constructor)?

  2. 2

    How can I create a subclass that inherit Ellipse class

  3. 3

    How can my subclass properly inherit a static variable

  4. 4

    Why interface cannot inherit pure abstract class

  5. 5

    How can we inherit UITableViewDataSource protocol in our class?

  6. 6

    How can I know what Classes inherit from a Class or Interface?

  7. 7

    How can I tell if my interface{} is a pointer?

  8. 8

    Since Interface cannot inherit Class, then how "Object" class methods available to the interface reference in java?

  9. 9

    How to inherit variables to class

  10. 10

    How to inherit from GObject class?

  11. 11

    Can and how service class inherit from plain class?

  12. 12

    Can a class inherit the properies of a namedtuple?

  13. 13

    How can a class inherit from a parameterized version of itself?

  14. 14

    Can not inherit from final class

  15. 15

    Can a C++ interface inherit from a class with full implementation?

  16. 16

    How can I make a class both implement an Interface and inherit from another class

  17. 17

    Why can not inherit from Interface

  18. 18

    How can I create a subclass that inherit Ellipse class

  19. 19

    How can I inherit class in CSS?

  20. 20

    Can a class inherit implementation of a method contruct imposed by an interface from other class?

  21. 21

    QT5.3 How can I add my Qobject to the QQmlEngine and access it's Properties via QML?

  22. 22

    How can I define some functions individually for each interface of my class?

  23. 23

    Does Qt Q_PROPERTY require my class to inherit QObject?

  24. 24

    Can and how service class inherit from plain class?

  25. 25

    how to avoid interface method in my class

  26. 26

    How can a template class inherit from a nested template class

  27. 27

    How to inherit a python base class?

  28. 28

    How to emit signals from a class that can't inhert QObject?

  29. 29

    How we can inherit BaseModel class in odoo

HotTag

Archive