本文介绍了具有虚函数的类,当从QObject派生时,导致链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是工作正常的代码

  class HttpService {
public:
virtual〜HttpService(); // implements in .cpp
protected:
HttpService(struct MHD_Connection * conn){}
};
class HttpFileService:public HttpService
{
public:
virtual〜HttpFileService(); // implements 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){}
};

我遇到以下链接错误:

 架构x86_64的未定义符号:
vtable for HttpService,引用自:
HttpService ::〜httpservice.o中的HttpService()

更改 HttpService 的构造函数不会帮助

 显式HttpService(QObject * parent = 0):QObject(parent)
pre>

解决方案

强制运行qmake,看看是否有效。


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)
解决方案

Force running qmake and see if it works.

这篇关于具有虚函数的类,当从QObject派生时,导致链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 20:08