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

相关推荐
RestCloud11 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud11 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence14 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger20 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud2 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试