Qt 对 vtable 的未定义引用

2021-12-13 00:00:00 user-interface qt4 c++ codeblocks

我是 Qt 编程的初学者,并使用代码块进行编程.我创建了3个文件communication.h、commmunicate.cpp和main.cpp,如下:

I am a beginner to Qt programming and use codeblocks for my programming. I created 3 files communicate.h,commmunicate.cpp and main.cpp as follows:

communicate.h

    #ifndef COMMUNICATE_H
    #define COMMUNICATE_H

    #include <QWidget>
    #include <QApplication>
    #include <QPushButton>
    #include <QLabel>

    class Communicate : public QWidget
    {
      Q_OBJECT

      public:
        Communicate(QWidget *parent = 0);


      private slots:
        void OnPlus();
        void OnMinus();

      private:
        QLabel *label;

    };

    #endif

communicate.cpp

#include "communicate.h"

Communicate::Communicate(QWidget *parent)
    : QWidget(parent)
{
  QPushButton *plus = new QPushButton("+", this);
  plus->setGeometry(50, 40, 75, 30);

  QPushButton *minus = new QPushButton("-", this);
  minus->setGeometry(50, 100, 75, 30);

  label = new QLabel("0", this);
  label->setGeometry(190, 80, 20, 30);

  connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
  connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
}

void Communicate::OnPlus()
{
  int val = label->text().toInt();
  val++;
  label->setText(QString::number(val));
}

void Communicate::OnMinus()
{
  int val = label->text().toInt();
  val--;
  label->setText(QString::number(val));
}

ma??in.cpp

#include "communicate.h"

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);

  Communicate window;

  window.setWindowTitle("Communicate");
  window.show();

  return app.exec();
}

及其显示错误如下:

objDebugmain.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0xb)||In function `ZN7QStringC1EPKc':|
C:Qt4.4.3includeQtCore....srccorelibarchqatomic_windows.h||undefined reference to `vtable for Communicate'|
objDebugmain.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0x17):C:Qt4.4.3includeQtCore....srccorelibarchqatomic_windows.h||undefined reference to `vtable for Communicate'|
objDebugcommunicate.o(.text+0x172)||In function `ZN11CommunicateC2EP7QWidget':|
E:Projectcam2communicate.cpp|5|undefined reference to `vtable for Communicate'|
objDebugcommunicate.o(.text+0x17e):E:Projectcam2communicate.cpp|5|undefined reference to `vtable for Communicate'|
objDebugcommunicate.o(.text+0x63a)||In function `ZN11CommunicateC1EP7QWidget':|
E:Projectcam2communicate.cpp|5|undefined reference to `vtable for Communicate'|
objDebugcommunicate.o(.text+0x646):E:Projectcam2communicate.cpp|5|more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|

伙计们请帮忙...无法弄清楚...

guys please help...cant figure it out...

推荐答案

这是我以前见过的一个微妙的错误(可能至少部分是编译器错误).由于 QWidget 有一个虚拟析构函数,编译器需要一个 vtable 用于您的类.但是你的类没有任何虚函数,所以它没有为你的 Communicate 类构建一个.

This is a subtle bug (and probably partly at least a compiler bug) that I've seen before. Since QWidget has a virtual destructor, the compiler needs a vtable for your class. But your class doesn't have any virtual functions, so it didn't build one for your Communicate class.

在你的类中添加一个 virtual ~Communicate() {};,一切都会好起来的.

Add a virtual ~Communicate() {}; to your class, and all will be well.

是的,我也花了一些时间才弄明白!

Yes, it took me some time to figure this out too!

相关文章