具有虚函数的类,当从 QObject 派生时,会导致链接错误
以下是运行良好的代码
class HttpService {
public:
virtual ~HttpService(); // implemented in .cpp
protected:
HttpService(struct MHD_Connection *conn) {}
};
class HttpFileService : public HttpService
{
public:
virtual ~HttpFileService() ; // implemented in .cpp
protected:
HttpFileService(struct MHD_Connection *conn) : HttpService(conn) {}
};
现在,当我将 HttpService
设为 QObject
的派生类时,如下所示:
#include <QObject> // change #1
class HttpService : public QObject { // change #2
Q_OBJECT // change #3
public:
virtual ~HttpService();
protected:
HttpService(struct MHD_Connection *conn) {}
};
class HttpFileService : public HttpService {
Q_OBJECT // change #4
public:
virtual ~HttpFileService() ;
protected:
HttpFileService(struct MHD_Connection *conn) : HttpService(conn) {}
};
我遇到以下链接错误:
Undefined symbols for architecture x86_64:
"vtable for HttpService", referenced from:
HttpService::~HttpService()in httpservice.o
更改 HttpService
code> 的构造函数对以下内容也没有帮助
explicit HttpService(QObject *parent = 0) : QObject(parent)
Following is the code that works fine
class HttpService {
public:
virtual ~HttpService(); // implemented in .cpp
protected:
HttpService(struct MHD_Connection *conn) {}
};
class HttpFileService : public HttpService
{
public:
virtual ~HttpFileService() ; // implemented in .cpp
protected:
HttpFileService(struct MHD_Connection *conn) : HttpService(conn) {}
};
Now, when I make HttpService
a derived class of QObject
, like below:
#include <QObject> // change #1
class HttpService : public QObject { // change #2
Q_OBJECT // change #3
public:
virtual ~HttpService();
protected:
HttpService(struct MHD_Connection *conn) {}
};
class HttpFileService : public HttpService {
Q_OBJECT // change #4
public:
virtual ~HttpFileService() ;
protected:
HttpFileService(struct MHD_Connection *conn) : HttpService(conn) {}
};
I encounter the following linking error:
Undefined symbols for architecture x86_64:
"vtable for HttpService", referenced from:
HttpService::~HttpService()in httpservice.o
Changing HttpService
's constructor to the following doesn't help either
explicit HttpService(QObject *parent = 0) : QObject(parent)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
强制运行 qmake 并查看它是否有效。
Force running qmake and see if it works.
您是否链接到正确的 qt 库?
Are you linking to the correct qt libraries?
你在调用 moc 编译器吗?如果没有,请删除 Q_OBJECT 宏!您是否包含/链接 moc 编译的结果?
Are you calling the moc-compiler? If not, remove the Q_OBJECT macros! And do you include / link the results from the moc-compilation?