S B
S B

Reputation: 8384

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

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)

Upvotes: 4

Views: 2965

Answers (3)

Jens
Jens

Reputation: 6329

Are you calling the moc-compiler? If not, remove the Q_OBJECT macros! And do you include / link the results from the moc-compilation?

Upvotes: 0

Soumya Das
Soumya Das

Reputation: 1665

Force running qmake and see if it works.

Upvotes: 10

Andreas Brinck
Andreas Brinck

Reputation: 52549

Are you linking to the correct qt libraries?

Upvotes: 1

Related Questions