下面是一个正常工作现在类具有虚拟功能,从继承自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)
代码更改后,'〜HttpService()'仍然在'.cpp'文件中实现?我问这是因为,在更改后的代码中,我没有看到注释为“//在.cpp中实现”。 – iammilind
@iammilind,是的,它仍然是。唯一被更改的行在注释中突出显示 –
是否是整个'HttpService()'?如果我在一个抽象基类中声明了一个虚函数,我经常会看到这个错误,并且忘记将其变为纯文本。 (gcc在与第一个非纯的非内联虚函数相同的目标文件中生成vtable,如果声明了这样的函数,并且没有定义,那么它最终会丢失)。 –