Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to vtable for ...

I am trying to write an Http proxy that basically works like indianwebproxy

So i fired up qtcreator and but one of my classes is failing to compile with the infamous error : undefined reference to vtable for HttpProxyThreadBrowser. I can't figure out why its doing this. I read through similar questions on Stackoverflow and apparently the problem is with undefined virtual methods that are not pure But i have not declared any virtual functions. Here is my class

class HttpProxyThreadBrowser : public QThread
{
public:
    HttpProxyThreadBrowser(QTcpSocket outgoingSocket,QTcpSocket  browserSocket,QObject *parent = 0);
    ~HttpProxyThreadBrowser(){};
    void run();

private:
    QTcpSocket outgoingSocket;
    QTcpSocket browserSocket;

};

And I define the class here in pastebin so as not to bore you. Unfortunately i cant find out why the vtable is undefined. Please assist.

httpproxythreadbrowser.cpp:5: undefined reference to `vtable for HttpProxyThreadBrowser
collect2: ld returned 1 exit status
like image 911
Dr Deo Avatar asked Mar 09 '12 13:03

Dr Deo


3 Answers

The destructor is implicitly virtual because a base class has a virtual d'tor.

The GNU compiler emits the vtable along with the first non-inline virtual method ("key method"). As your d'tor is defined inside the class, it is implicitly virtual, and as there are no other virtual methods, you don't have a key method.

There is no use case where a concrete class would have only virtual inline methods, as they can be inlined only into derived classes.

I'd move the definition of the dtor to the implementation file.

I'm not sure whether you need to use moc here as well, or if QThread derivatives work without (IIRC you need it only for Qt's cast operators, and for signals/slots).

like image 184
Simon Richter Avatar answered Oct 05 '22 12:10

Simon Richter


I had also a undefined reference to vtable error and followed the steps in Undefined reference to vtable... Q_OBJECT macro, that adviced me to run qmake and... it worked!

like image 30
Sys Avatar answered Oct 05 '22 12:10

Sys


You can't copy QTcpSockets, so it may cause other cryptic errors if you try to pass them by copy rather than by address.

    HttpProxyThreadBrowser(QTcpSocket * outgoingSocket,QTcpSocket * browserSocket,QObject *parent = 0);

private:
    QTcpSocket* outgoingSocket;
    QTcpSocket* browserSocket;

And completely recompiling your project may help, when you change header files, because qmake generated Makefile can sometimes fail to notice the changes.

like image 35
alexisdm Avatar answered Oct 05 '22 13:10

alexisdm