Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class with virtual function, when derived from QObject, leads to linking error

Tags:

c++

linker

qt

ld

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)
like image 726
S B Avatar asked Dec 21 '22 09:12

S B


2 Answers

Force running qmake and see if it works.

like image 112
Soumya Das Avatar answered Dec 24 '22 02:12

Soumya Das


Are you linking to the correct qt libraries?

like image 43
Andreas Brinck Avatar answered Dec 24 '22 02:12

Andreas Brinck