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 小时前
ECONNREFUSED ::1:8000 前端代理问题
开发语言
cike_y20 小时前
Mybatis之解析配置优化
java·开发语言·tomcat·mybatis·安全开发
云老大TG:@yunlaoda36021 小时前
华为云国际站代理商TaurusDB的成本优化体现在哪些方面?
大数据·网络·数据库·华为云
TG:@yunlaoda360 云老大21 小时前
华为云国际站代理商GeminiDB的企业级高可用具体是如何实现的?
服务器·网络·数据库·华为云
Jay_Franklin21 小时前
SRIM通过python计算dap
开发语言·python
Slow菜鸟21 小时前
Java基础架构设计(三)| 通用响应与异常处理(分布式应用通用方案)
java·开发语言
消失的旧时光-194321 小时前
401 自动刷新 Token 的完整架构设计(Dio 实战版)
开发语言·前端·javascript
wadesir21 小时前
Rust中的条件变量详解(使用Condvar的wait方法实现线程同步)
开发语言·算法·rust
tap.AI21 小时前
RAG系列(二)数据准备与向量索引
开发语言·人工智能
阿蒙Amon1 天前
C#每日面试题-重写和重载的区别
开发语言·c#