具有虚函数的类,从QObject派生时,会导致链接错误

时间:2011-08-18 07:41:09

标签: c++ qt linker ld

以下是运行良好的代码

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的构造函数更改为以下内容对

无效
explicit HttpService(QObject *parent = 0) : QObject(parent)

3 个答案:

答案 0 :(得分:10)

强制运行qmake并查看它是否有效。

答案 1 :(得分:1)

您是否链接到正确的qt库?

答案 2 :(得分:0)

你在调用moc编译器吗?如果没有,删除Q_OBJECT宏!你是否包含/链接moc-compilation的结果?