简体   繁体   English

未定义对“ vtable for MyCalss”的引用

[英]undefined reference to `vtable for MyCalss'

I've created new project with QTCreator using Empty Qt Project 我已经使用Empty Qt Project用QTCreator创建了新项目

MocTest.pro MocTest.pro

QT += core
QT -= gui

TARGET = MocTest
CONFIG   += console

TEMPLATE = app


SOURCES += \
    main.cpp

main.cpp main.cpp

#include <QCoreApplication>
#include <iostream>

class A : public QObject
{
    Q_OBJECT

public:
    A() {}

public slots:
    void slotA() {std::cout<<"Hi!";}
};

class B : public QObject
{
    Q_OBJECT

public:
    B () {}

    void doB() {emit ss();}

signals:
    void ss();
};

int main(int argc, char *argv[])
{
    QCoreApplication app (argc, argv);
    A a;
    B b;
    return app.exec();
}

I got errors 我有错误

D:\\Test\\MocTest\\debug\\main.o:-1: In function `ZN1AC1Ev': D:\\ Test \\ MocTest \\ debug \\ main.o:-1:在功能'ZN1AC1Ev'中:

D:\\Test\\MocTest\\main.cpp:9: error: undefined reference to `vtable for A' D:\\ Test \\ MocTest \\ main.cpp:9:错误:未定义对“ vtable for A”的引用

D:\\Test\\MocTest\\main.cpp:20: error: undefined reference to `vtable for B' D:\\ Test \\ MocTest \\ main.cpp:20:错误:未定义对“ vtable for B”的引用

D:\\Test\\MocTest\\main.cpp:4: error: undefined reference to `vtable for A' D:\\ Test \\ MocTest \\ main.cpp:4:错误:未定义对“ vtable for A”的引用

D:\\Test\\MocTest\\main.cpp:15: error: undefined reference to `vtable for B' D:\\ Test \\ MocTest \\ main.cpp:15:错误:未定义对“ vtable for B”的引用

collect2.exe:-1: error: error: ld returned 1 exit status collect2.exe:-1:错误:错误:ld返回了1个退出状态

Qmake ensures Moc is run on: Qmake确保Moc运行在:

  • Files listed in the HEADERS variable. HEADERS变量中列出的文件。 Qmake arranges to compile and link the resulting C++ implementation file by adding it to SOURCES. Qmake通过将生成的C ++实现文件添加到SOURCES来进行编译和链接。
  • Files listed in the SOURCES or OBJECTIVE_SOURCES variables are processed by moc. SOURCES或OBJECTIVE_SOURCES变量中列出的文件由moc处理。 Qmake does not link any potential output, so the resulting moc output must be #included. Qmake不链接任何潜在的输出,因此必须将#moc的输出结果包括在内。

Looks like you need to put this: 看来您需要输入以下内容:

#include "main.moc"

somewhere at main.cpp (probably at the end). main.cpp某个位置(可能在末尾)。 Details here: https://qt-project.org/forums/viewthread/21420 此处的详细信息: https : //qt-project.org/forums/viewthread/21420

vtable references are generated by moc for class heriting QObject . vtable引用是由moc为类继承QObject生成的。 These are generated with files from HEADERS variable. 这些是使用HEADERS变量中的文件生成的。 Then you have to separate headers and implementations. 然后,您必须分离标题和实现。 This would gives: 这将给出:

myclass.h myclass.h

class A : public QObject
{
    Q_OBJECT

public:
    A();
public slots:
    void slotA();
};

class B : public QObject
{
    Q_OBJECT

public:
    B();
    void doB();
signals:
    void ss();
};

and myclass.cpp 和myclass.cpp

#include "myclass.h"

A::A()
{}
void A::slotA()
{ std::coud << "Hi!; }

B::B()
{}
void B::doB
{ emit ss(); }

Also then TestMoc.pro 然后再TestMoc.pro

QT = core # default is QT = core gui, so -= gui is enough or just =core
HEADERS = myclass.h
SOURCES = myclass.cpp main.cpp

Keep in mind that it generally a bad idea to call signals in inline methods. 请记住,以内联方法调用信号通常不是一个好主意。 Also if you call doB in your main, before the app.exec(), it might not be handle because signals/slots are managed once the event-loop is run 另外,如果您在主程序中在app.exec()之前调用doB,则可能无法处理,因为一旦运行事件循环,便会管理信号/插槽

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM