qt: undefined reference to `vtable for aaa‘

版本qt4.8.6,编译报错"main.cpp:(.text+0x3b): undefined reference to `vtable for aaa'"

就一个main.cpp

cpp 复制代码
#include <QApplication>
#include <QTimer>
#include <QCursor>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <qglobal.h>

class aaa:public QApplication
{
    Q_OBJECT
public:
    aaa(int argc, char *argv[]):QApplication(argc,argv){}
private slots:
    void moveMouse()
    {
        QPoint point(100, 100); // 设置鼠标移动的目标位置
        QMouseEvent mouseEvent(QEvent::MouseMove, getRandomPosition(), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
        QApplication::sendEvent(QApplication::desktop(), &mouseEvent);
        qDebug() << "Mouse moved to" << getRandomPosition();
    }
    QPoint getRandomPosition() {
    int screenWidth = QDesktopWidget().width();
    int screenHeight = QDesktopWidget().height();
    return QPoint(qrand()%screenWidth, qrand()%screenHeight);
}
};
int main(int argc, char *argv[]) {
    aaa app(argc, argv);
    QTimer timer;
    QObject::connect(&timer, SIGNAL(timeout()), &app, SLOT(moveMouse()));
    timer.start(1000*60*10);
    return  app.exec();
}
#include "main.moc"   //这一句很关键

由于类aaa使用了信号槽,并且写在源文件中,致使找不到main.moc,需要在文件末尾主动包含"main.moc"

使用 #include "main.moc" 或者类似的包含 .moc 文件的情况,通常出现在以下两种情况:

  1. 当你在.cpp源文件中定义了一个Q_OBJECT宏的类时。
  2. 当你手动调用 moc 命令来处理一个头文件,并将生成的源文件包含到你的项目中时。

如果你没有在 .cpp 文件中定义 Q_OBJECT 宏的类,并且没有手动使用 moc,那么你不需要包含 .moc 文件。通常,在用 qmakeCMake 等构建系统生成的项目中,.moc 文件会自动被处理和包含,不需要你手动包含。

为了避免以上问题出现,如果使用率Q_OBJECT宏,该类必须在头文件声明

相关推荐
charlie11451419111 分钟前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
IT项目管理33 分钟前
达梦数据库DMHS介绍及安装部署
linux·数据库
你都会上树?40 分钟前
MySQL MVCC 详解
数据库·mysql
大春儿的试验田1 小时前
高并发收藏功能设计:Redis异步同步与定时补偿机制详解
java·数据库·redis·学习·缓存
likeGhee1 小时前
python缓存装饰器实现方案
开发语言·python·缓存
whoarethenext1 小时前
使用 C++/Faiss 加速海量 MFCC 特征的相似性搜索
开发语言·c++·faiss
项目題供诗1 小时前
黑马python(二十五)
开发语言·python
Ein hübscher Kerl.1 小时前
虚拟机上安装 MariaDB 及依赖包
数据库·mariadb
慌糖1 小时前
RabbitMQ:消息队列的轻量级王者
开发语言·javascript·ecmascript
醇醛酸醚酮酯2 小时前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt