Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Linker Error: "undefined reference to vtable" [duplicate]

This is my header:

#ifndef BARELYSOCKET_H #define BARELYSOCKET_H  #include <QObject> //! The First Draw of the BarelySocket!  class BarelySocket: public QObject {     Q_OBJECT  public:     BarelySocket(); public slots:     void sendMessage(Message aMessage); signals:     void reciveMessage(Message aMessage);  private:     //   QVector<Message> reciveMessages; };  #endif // BARELYSOCKET_H 

This is my class:

#include <QTGui> #include <QObject> #include "type.h" #include "client.h" #include "server.h"  #include "barelysocket.h"  BarelySocket::BarelySocket() {     //this->reciveMessages.clear();     qDebug("BarelySocket::BarelySocket()"); }  void BarelySocket::sendMessage(Message aMessage) { }  void BarelySocket::reciveMessage(Message aMessage) { } 

I get a Linker error:

undefined reference to 'vtable for BarelySocket' 
  • This implies that I have a virtual method not implemented. But there are no virtual methods in my class.
  • I commented out the vector thinking that it was the cause, but the error did not go away.
  • The Message is a complex struct, but even using int instead did not fix things.
like image 848
Thomas Avatar asked Mar 31 '10 19:03

Thomas


People also ask

What does undefined reference to Vtable mean?

In summary, there are three key causes of the "undefined reference to vtable" error: A member function is missing its definition. An object file is not being linked. All virtual functions have inline definitions.

How do you resolve undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you fix undefined references to Main?

When we compile these files separately, the first file gives “undefined reference” for the print function, while the second file gives “undefined reference” for the main function. The way to resolve this error is to compile both the files simultaneously (For example, by using g++).

What is Q_object?

QObject is the base class for all Qt classes, Q_OBJECT macro is used to enable meta-object features in classes and finally moc is a preprocessor that changes Q_OBJECT macro instances to C++ source code to enable meta object system mechanism in the class in which it is used.


1 Answers

Any time you add a new call to the Q_OBJECT macro, you need to run qmake again. The vtables issue you're referring to is directly related to that.

Just run qmake and you should be good to go assuming there are no other issues in your code.

like image 125
Michael Avatar answered Oct 09 '22 12:10

Michael