Qt未定义对vtable的引用 [英] Qt undefined reference to vtable

查看:1177
本文介绍了Qt未定义对vtable的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者Qt编程和使用代码块我的编程。我创建了3个文件communications.h,commmunicate.cpp和main.cpp如下:



communic.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);


私有插槽:
void OnPlus();
void OnMinus();

private:
QLabel * label;

};

#endif

communicate.cpp / p>

  #includecommunic.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()。
val ++;
label-> setText(QString :: number(val));
}

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

main.cpp
$ b

  #includecommunic.h

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

通信窗口;

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

return app.exec();
}

及其显示错误如下:

  obj \Debug\main.o(.text $ _ZN11CommunicateD1Ev [Communicate ::〜Communicate()] + 0xb)||函数`ZN7QStringC1EPKc':| 
C:\Qt\4.4.3\include\QtCore\..\..\src\corelib\arch\qatomic_windows.h ||未定义的引用`vtable for沟通
obj\Debug\main.o(.text $ _ZN11CommunicateD1Ev [Communicate ::〜Communicate()] + 0x17):C:\Qt\4.4.3\include\QtCore\。 .\..\src\corelib\arch\qatomic_windows.h ||未定义的引用`vtable for Communicate'|
obj\Debug\communicate.o(.text + 0x172)||在函数`ZN11CommunicateC2EP7QWidget':|
E:\Project\cam2\communicate.cpp | 5 |未定义引用`vtable for Communicate'|
obj\Debug\communicate.o(.text + 0x17e):E:\Project\cam2\communicate.cpp | 5 |未定义引用`vtable for Communicate'|
obj\Debug\communicate.o(.text + 0x63a)||在函数`ZN11CommunicateC1EP7QWidget':|
E:\Project\cam2\communicate.cpp | 5 |未定义引用`vtable for Communicate'|
obj\Debug\communicate.o(.text + 0x646):E:\Project\cam2\communicate.cpp | 5 |更多未定义的引用`vtable for Communicate'跟随|
|| === Build finished:6 errors,0 warnings === |

家伙请帮忙...难以置信...

解决方案

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



向您的班级添加 virtual〜Communicate(){}; p>

是的,我花了一些时间来弄清楚这一点!


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));
}

main.cpp

#include "communicate.h"

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

  Communicate window;

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

  return app.exec();
}

and its showing errors as follows:

obj\Debug\main.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0xb)||In function `ZN7QStringC1EPKc':|
C:\Qt\4.4.3\include\QtCore\..\..\src\corelib\arch\qatomic_windows.h||undefined reference to `vtable for Communicate'|
obj\Debug\main.o(.text$_ZN11CommunicateD1Ev[Communicate::~Communicate()]+0x17):C:\Qt\4.4.3\include\QtCore\..\..\src\corelib\arch\qatomic_windows.h||undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x172)||In function `ZN11CommunicateC2EP7QWidget':|
E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x17e):E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x63a)||In function `ZN11CommunicateC1EP7QWidget':|
E:\Project\cam2\communicate.cpp|5|undefined reference to `vtable for Communicate'|
obj\Debug\communicate.o(.text+0x646):E:\Project\cam2\communicate.cpp|5|more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|

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

解决方案

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.

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

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

这篇关于Qt未定义对vtable的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆