Search code examples
c++qtsignals-slotsqobject

qt - undefined reference to `vtable for myObj' in qt console application - signals and slots


I need to capture emited signals from a QProcess for testing purposes.

Since I am using a console application, I resolved to create a class in my main.cpp file called myObj using mainly this example:

#include <QCoreApplication>
#include <QLoggingCategory>
#include <QTextStream>
#include <QProcess>
#include <QString>
#include <QVariant>
#include <QDebug>
#include <QObject>

class myObj : public QObject
{

    Q_OBJECT

public:
    myObj(QObject *parent = 0);
//    virtual ~Communicate();
    ~myObj();

public slots:
    void registerFinished(int signal);
    void registerAboutToClose();
    void registerChannelReadyRead(int signal);
    void registerReadChannelFinished();
    void registerReadyRead();
    void registerReadyReadStandardOutput();
    void registerStarted();
};

myObj::myObj(QObject *parent)
    : QObject(parent)             <--- LINE 72 Error
{

}

//virtual myObj::~Communicate(){

//}

myObj::~myObj(){                  <--- LINE 81 Error

}

void myObj::registerFinished(int signal){
    qDebug() << "exit code = " << QString::number(signal);
}
void myObj::registerAboutToClose(){
    qDebug() << "aboutToClose";
}
void myObj::registerChannelReadyRead(int signal){
    qDebug() << "channelReadyRead = " << QString::number(signal);
}
void myObj::registerReadChannelFinished(){
    qDebug() << "readChannelFinished";
}
void myObj::registerReadyRead(){
    qDebug() << "exit code";
}
void myObj::registerReadyReadStandardOutput(){
    qDebug() << "exit code";
}
void myObj::registerStarted(){
    qDebug() << "started";
}

myObj *myO;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    myO = new myObj();

    //....
}

Problem:

main.cpp:72: error: undefined reference to `vtable for myObj'

main.cpp:81: error: undefined reference to `vtable for myObj'

I have looked at a number of SO pages e.g here and here and here and various others, yet had not found a solution

I have tried/done:

  • added the Q_Object Macro
  • ran qmake
  • rebuilt
  • checked the #include

.pro file

QT += core
QT -= gui

CONFIG += c++11

TARGET = serv_app
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

Any suggestions?


Solution

  • You have two options:

    • @eyllanesc solution works for sure.
    • add the line #include "main.moc" before or after you main() function.

    When you put the class into its own header file, qmake will generate the proper moc file.

    But when you put the class into a .cpp file, the moc code is not generated unless you put the line I said before.

    Update #1

    In the Qt tutorial about writing a Unit Test we can find the following info:

    Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.

    So this is another example where we need to include the moc file.