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

问题描述

我收到此错误

  /.../ mainwindow.o:-1:在MainWindow :: MainWindow (QWidget *)':
/.../mainwindow.cpp:-1:Chyba:未定义引用`vtable for Msnger'

我不明白为什么。



这里是类Msnger
msnger.h:

/ p>

  #include< QObject> 

class Msnger:public QObject
{
Q_OBJECT

public:
Msnger(){};
〜Msnger(){};
void sendOn();
signals:
void ton(){};
};

msnger.cpp:

  #includemsnger.h

void Msnger :: sendOn()
{
emit ton
}

Msnger应该向我的Mainwindow发送消息

$ b $ mainWindow构造函数中的b

  msn = new Msnger 
connect(msn,SIGNAL(ton()),this,SLOT(on()));

其中msn是:

  public:Msnger * msn 


解决方案

您的问题是 moc 没有在你的文件上运行,或者其结果没有被链接。



黄金规则是:


  1. 确保 Q_OBJECT 宏存在于所有 QObject - 派生类的定义中。

  2. 确保您只在的头文件中声明 QObject

  3. 请确保您的.pro文件中的 HEADERS = 列表中列出了所有头文件。

  4. 每次向您的课程添加 Q_OBJECT 时,请运行 qmake 修改您的 .pro 文件。






附录:

 信号:
void ton

不要实施信号。 moc 将为您实现。


I get this error

/.../mainwindow.o:-1: In function `MainWindow::MainWindow(QWidget*)':
/.../mainwindow.cpp:-1: Chyba:undefined reference to `vtable for Msnger'

and i dont understand why. Only problems i found causing this message are declaration without definition and i dont see any of theese in my code.

here is class Msngermsnger.h:

#include <QObject>

class Msnger : public QObject
{
    Q_OBJECT

public:
    Msnger() {};
    ~Msnger() {};
    void sendOn();
signals:
    void ton() {};
};

msnger.cpp:

#include "msnger.h"

void Msnger::sendOn()
{
    emit ton();
}

Msnger is supposed to send message to my Mainwindow

in constructor of mainWindow:

msn = new Msnger();
connect(msn, SIGNAL(ton()),this, SLOT(on()));

where msn is:

public: Msnger * msn

can you pls explain to me what's causing this and how can I fix it?

解决方案

Your problem is that moc is not being run on your files, or its result is not getting linked.

The golden rules are:

  1. Make sure the Q_OBJECT macro is present in the definition of all QObject-derived classes.
  2. Make sure you declare your QObject-derived classes in your header files only.
  3. Make sure all of your header files are listed in your .pro file in the HEADERS= list.
  4. Run qmake every time you add Q_OBJECT to one of your classes or modify your .pro file.


Addendum:

signals:
    void ton() {};

Do not implement a signal. moc will implement it for you.

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

08-24 17:23
查看更多