2011-08-18 48 views
4

下面是一个正常工作现在类具有虚拟功能,从继承自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的构造以下内容并没有帮助

explicit HttpService(QObject *parent = 0) : QObject(parent) 
+1

代码更改后,'〜HttpService()'仍然在'.cpp'文件中实现?我问这是因为,在更改后的代码中,我没有看到注释为“//在.cpp中实现”。 – iammilind

+0

@iammilind,是的,它仍然是。唯一被更改的行在注释中突出显示 –

+2

是否是整个'HttpService()'?如果我在一个抽象基类中声明了一个虚函数,我经常会看到这个错误,并且忘记将其变为纯文本。 (gcc在与第一个非纯的非内联虚函数相同的目标文件中生成vtable,如果声明了这样的函数,并且没有定义,那么它最终会丢失)。 –

回答

9

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

+0

哇!

1

你链接到正确的Qt库?

+0

是的,在我的项目中还有其他的QObject派生类正在经历很好 –

0

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