[英]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. 当我取出QObject的继承关系时,它就起作用了。
Have any idea how I can do that ? 有任何想法我该怎么做吗?
That's probably because you aren't including the .moc
file in the build. 那可能是因为您没有在构建中包括
.moc
文件。 See Qt Linker Error: "undefined reference to vtable" for a similar problem, although, provided that your build system is unknown, the idea is that: 有关类似问题,请参见Qt Linker错误:“对vtable的未定义引用” ,但是,如果您的构建系统未知,则想法是:
a) you need to make sure moc
is ran on your .cpp
file and a)您需要确保在
.cpp
文件上运行了moc
并且
b) that the resulting .cpp
file is included in the build. b)生成的
.cpp
文件包含在构建中。
How exactly it is done, depends on the build system. 具体的完成方式取决于构建系统。 For instance, in my current project, with Cmake 3.xx, this command is enough:
例如,在我当前的项目中,使用Cmake 3.xx,此命令就足够了:
set (CMAKE_INCLUDE_CURRENT_DIR ON)
set (CMAKE_AUTOMOC ON)
For GNU make
, here is an example of how it can be done: 对于GNU
make
,这是一个如何完成的示例:
http://doc.qt.io/qt-4.8/moc.html#writing-make-rules-for-invoking-moc http://doc.qt.io/qt-4.8/moc.html#writing-make-rules-for-invoking-moc
For qmake
, see eg 对于
qmake
,请参见例如
Why is important to include ".moc" file at end of a Qt Source code file? 为什么在Qt源代码文件的末尾包含“ .moc”文件很重要?
As for multiple inheritance, that's not allowed if multiple QObject
's are to be inherited. 对于多重继承,如果要继承多个
QObject
不允许这样做。 On the contrary, when there is one QObject
and multiple "conventional" classes, that works fine. 相反,当有一个
QObject
和多个“常规”类时,它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.