Qt undefined reference to `vtable for Msnger'

给你一囗甜甜゛ 提交于 2019-12-11 19:47:29

问题


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 Msnger msnger.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?


回答1:


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.




回答2:


Not sure about your compiler.

Make sure that Qt Meta compiler is running for the header file where Msnger class is defined. And the resulting moc_* cpp file is compiled with your compiler.

PS. It's good when QObject inherited classes names start with 'Q' e.g. QMsnger



来源:https://stackoverflow.com/questions/19742466/qt-undefined-reference-to-vtable-for-msnger

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!