以下是能正常工作的代码
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
文件中是否仍有实现?我之所以问这个问题是因为在更改后的代码中,我没有看到// implemented in .cpp
这样的注释。 - iammilindHttpService()
的全部内容吗?如果我在抽象基类中声明了一个虚函数并忘记将其定义为纯虚函数,那么我经常会看到这个错误。(如果声明了一个非纯、非内联虚函数,并且如果gcc在同一对象文件中生成vtable,那么如果该函数没有定义,则它将缺失)。 - Mike Seymour