问题描述
最近我需要向类添加信号,因此我将类更改为从QObject继承,并将Q_OBJECT宏添加到类定义中.这样做之后,我在下面的类行中收到'vtable for CLICommand'的信号未定义参考错误" 错误:
I recently needed to add a signal to a class, so I changed the class to inherit from QObject and added the Q_OBJECT macro into the class definition. Since doing so I get "signal undefined reference error for 'vtable for CLICommand'" error on the class line below:
// File clicommand.h
#include <QString>
#include <QStringList>
#include <QTcpSocket>
#include "telnetthread.h"
class CLICommand : public QObject
{
Q_OBJECT
public:
CLICommand(TelnetThread *parentTelnetThread);
signals:
void signal_shutdown_request();
private:
TelnetThread *m_parentTelnetThread;
和第二行错误用于CLICommand的vtable的信号未定义参考错误" (初始化成员变量):
and the second error "signal undefined reference error for 'vtable for CLICommand'" on the line below (intializing the member variable):
// File clicommand.cpp
#include <QDebug>
#include <QTcpSocket>
#include <QTextStream>
#include "version.h"
#include "clicommand.h"
#include "telnetthread.h"
#include "logger.h"
CLICommand::CLICommand(TelnetThread *parentTelnetThread)
: m_parentTelnetThread(parentTelnetThread)
{
}
而这就是我发出信号的地方.发射行生成错误对`CLICommand :: signal_shutdown_request()'的未定义引用:
and just here is where I emit the signal. The emit line generates error undefined reference to `CLICommand::signal_shutdown_request()' :
// file shutdown_clicommand.cpp
#include <QIODevice>
#include "clicommand.h"
#include "logger.h"
#include "version.h"
void CLICommand::execute_shutdown(const QStringList &commandLineFragments)
{
emit signal_shutdown_request();
}
我已经阅读了很多有关该主题的文章,但似乎都没有.我什至尝试清理/重建.我没有使用boost或其他库...只是QT 5
I've read a bunch of posts on this topic but none seem to apply. I even tried clean/rebuildall. I am not using boost or other libraries...just QT 5
有人可以告诉我我做错了什么吗?
Can someone tell me what I'm doing wrong?
解决方案:在QT Creator中,右键单击该项目,选择RUN QMAKE,然后重新生成所有内容.关于运行REBUILD ALL的其他帖子是不正确的...单独运行qmake不会.
SOLUTION: in QT Creator, right click the project, select RUN QMAKE, then rebuild all. Other posts about running REBUILD ALL are incorrect...on it's own that will NOT run qmake.
推荐答案
您需要确保针对这些更改运行"moc".看来您要么错过了生成的moc文件,要么该文件已经过时,因此没有包含对当前状态的正确引用.
You need to make sure 'moc' is (re)run for those changes. It seems that you either miss the generated moc file, or that is outdated and hence not containing the proper reference to the current state.
QtCreator无法正确地重新运行qmake
.这是一个长期存在的问题,因此您需要手动重新运行它.
QtCreator does not rerun qmake
properly when the Q_OBJECT macro is added. This has been a long standing issue, so you will need to rerun it manually.
https://bugreports.qt.io/browse/QTCREATORBUG-231
这篇关于Qt“信号未定义的参考错误"在图2中示出.从QObject继承之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!