qt - 在 qt 控制台应用程序 - 信号和槽中未定义对“vtable for myObj”的引用
qt - undefined reference to `vtable for myObj' in qt console application - signals and slots
出于测试目的,我需要从 QProcess 捕获发出的信号。
由于我使用的是控制台应用程序,因此我决定在名为 myObj
的 main.cpp 文件中创建一个 class,主要使用 this example:
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QTextStream>
#include <QProcess>
#include <QString>
#include <QVariant>
#include <QDebug>
#include <QObject>
class myObj : public QObject
{
Q_OBJECT
public:
myObj(QObject *parent = 0);
// virtual ~Communicate();
~myObj();
public slots:
void registerFinished(int signal);
void registerAboutToClose();
void registerChannelReadyRead(int signal);
void registerReadChannelFinished();
void registerReadyRead();
void registerReadyReadStandardOutput();
void registerStarted();
};
myObj::myObj(QObject *parent)
: QObject(parent) <--- LINE 72 Error
{
}
//virtual myObj::~Communicate(){
//}
myObj::~myObj(){ <--- LINE 81 Error
}
void myObj::registerFinished(int signal){
qDebug() << "exit code = " << QString::number(signal);
}
void myObj::registerAboutToClose(){
qDebug() << "aboutToClose";
}
void myObj::registerChannelReadyRead(int signal){
qDebug() << "channelReadyRead = " << QString::number(signal);
}
void myObj::registerReadChannelFinished(){
qDebug() << "readChannelFinished";
}
void myObj::registerReadyRead(){
qDebug() << "exit code";
}
void myObj::registerReadyReadStandardOutput(){
qDebug() << "exit code";
}
void myObj::registerStarted(){
qDebug() << "started";
}
myObj *myO;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myO = new myObj();
//....
}
问题:
main.cpp:72: error: undefined reference to `vtable for myObj'
main.cpp:81: error: undefined reference to `vtable for myObj'
我查看了许多 SO 页面,例如 here and and here 和其他各种页面,但还没有找到解决方案
我有tried/done:
- 添加了 Q_Object 宏
- 运行 qmake
- 重建
- 检查了#include
.pro 文件
QT += core
QT -= gui
CONFIG += c++11
TARGET = serv_app
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
有什么建议吗?
您有两个选择:
- @eyllanesc 解决方案肯定有效。
- 在
main()
函数之前或之后添加行 #include "main.moc"
。
当您将 class 放入其自己的头文件中时,qmake
将生成正确的 moc
文件。
但是当你把 class 放到一个 .cpp
文件中时,moc
代码不会生成,除非你把我之前说的那行。
更新#1
在Qt tutorial about writing a Unit Test中我们可以找到以下信息:
Note that if both the declaration and the implementation of our test
class are in a .cpp file, we also need to include the generated moc
file to make Qt's introspection work.
所以这是我们需要包含 moc
文件的另一个示例。
出于测试目的,我需要从 QProcess 捕获发出的信号。
由于我使用的是控制台应用程序,因此我决定在名为 myObj
的 main.cpp 文件中创建一个 class,主要使用 this example:
#include <QCoreApplication>
#include <QLoggingCategory>
#include <QTextStream>
#include <QProcess>
#include <QString>
#include <QVariant>
#include <QDebug>
#include <QObject>
class myObj : public QObject
{
Q_OBJECT
public:
myObj(QObject *parent = 0);
// virtual ~Communicate();
~myObj();
public slots:
void registerFinished(int signal);
void registerAboutToClose();
void registerChannelReadyRead(int signal);
void registerReadChannelFinished();
void registerReadyRead();
void registerReadyReadStandardOutput();
void registerStarted();
};
myObj::myObj(QObject *parent)
: QObject(parent) <--- LINE 72 Error
{
}
//virtual myObj::~Communicate(){
//}
myObj::~myObj(){ <--- LINE 81 Error
}
void myObj::registerFinished(int signal){
qDebug() << "exit code = " << QString::number(signal);
}
void myObj::registerAboutToClose(){
qDebug() << "aboutToClose";
}
void myObj::registerChannelReadyRead(int signal){
qDebug() << "channelReadyRead = " << QString::number(signal);
}
void myObj::registerReadChannelFinished(){
qDebug() << "readChannelFinished";
}
void myObj::registerReadyRead(){
qDebug() << "exit code";
}
void myObj::registerReadyReadStandardOutput(){
qDebug() << "exit code";
}
void myObj::registerStarted(){
qDebug() << "started";
}
myObj *myO;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myO = new myObj();
//....
}
问题:
main.cpp:72: error: undefined reference to `vtable for myObj'
main.cpp:81: error: undefined reference to `vtable for myObj'
我查看了许多 SO 页面,例如 here and
我有tried/done:
- 添加了 Q_Object 宏
- 运行 qmake
- 重建
- 检查了#include
.pro 文件
QT += core
QT -= gui
CONFIG += c++11
TARGET = serv_app
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
有什么建议吗?
您有两个选择:
- @eyllanesc 解决方案肯定有效。
- 在
main()
函数之前或之后添加行#include "main.moc"
。
当您将 class 放入其自己的头文件中时,qmake
将生成正确的 moc
文件。
但是当你把 class 放到一个 .cpp
文件中时,moc
代码不会生成,除非你把我之前说的那行。
更新#1
在Qt tutorial about writing a Unit Test中我们可以找到以下信息:
Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.
所以这是我们需要包含 moc
文件的另一个示例。