Qt 链接器错误:“对 vtable 的未定义引用"

2021-12-09 00:00:00 qt linker-errors c++ vtable qobject

这是我的标题:

#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

这是我的课:

#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)
{
}

我收到链接器错误:

undefined reference to 'vtable for BarelySocket'

  • 这意味着我有一个没有实现的虚方法.但是有我的类中没有虚拟方法.
  • 我注释掉了向量,认为这是原因,但是错误并没有消失.
  • Message 是一个复杂的 struct,但即使使用 int不解决问题.
    • 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.
    • 推荐答案

      任何时候添加对 Q_OBJECT 宏的新调用,都需要再次运行 qmake.您所指的 vtables 问题与此直接相关.

      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.

      只要运行 qmake,假设您的代码中没有其他问题,您应该会很高兴.

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

相关文章