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宏,该类必须在头文件声明

相关推荐
极限实验室20 小时前
INFINI Labs 产品更新 - Easysearch 2.1.0 新增高性能 Rules 规则引擎插件,数据探索 Discover 等
数据库·产品
globaldomain21 小时前
什么是用于长距离高速传输的TCP窗口扩展?
开发语言·网络·php
沈阳信息学奥赛培训1 天前
#undef 指令 (C/C++)
c语言·开发语言·c++
2401_873204651 天前
分布式系统安全通信
开发语言·c++·算法
鹿角片ljp1 天前
苍穹外卖 day05:店铺营业状态设置与Redis入门实战
数据库·redis·缓存
m0_743470371 天前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
Demon_Hao1 天前
JAVA通过Redis实现Key分区分片聚合点赞、收藏等计数同步数据库,并且通过布隆过滤器防重复点赞
java·数据库·redis
Dxy12393102161 天前
JS发送请求的方法详解
开发语言·javascript·ecmascript
sw1213891 天前
C++中的代理模式实战
开发语言·c++·算法
難釋懷1 天前
Lua语法入门-条件控制、函数
开发语言·junit·lua