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?



  • Hi, the Q_OBJECT macro in your class declaration needs to be processed by the moc compiler. Easiest is to move your class declaration into your main.h.
    EDIT: forgot probably you don't have a main.h yet, if so, just create it and put the class declaration inside it. And don't forget to #include main.h in your main.cpp :-)



  • Hi CybeX,
    I have experimented your program. All the syntax are perfect, there is a issue with CLASS name (myObj).
    with class name myObj i m getting linker error.
    But by changing the class name and building there is no error. So may be there is some dependency issue with class name myObj.
    change it to myObj1 and try.



  • @yuvaram

    Hi.

    You can just add the following line in main.cpp:

    [...]
    #include "main.moc"
    
    int main(int argc, char *argv[])
    {
    [...]
    
    

    and then run qmake.



  • @Devopia53
    Thank for correcting.

    But after adding #include "main.moc", (run qmake is also done) following error while building.

    Error: dependent 'debug\main.moc' does not exist.



  • @yuvaram

    Really? :(

    I'm works fine.
    If so, you can delete all of the previously built directories and then save main.cpp and rebuild all. Also, check where the main.moc file is generated.

    Good luck!.


  • Lifetime Qt Champion

    Hi,

    If you really want to use that technique, the moc include should be the last line of main.cpp file



  • Hi ,
    main.moc file is not generating.


  • Qt Champions 2016

    @yuvaram
    Hi
    For less pain. just use a .h file. :)

    Delete the build folder and rebuild all.



  • @yuvaram

    If so, compile it with Network Download Example, which is officially available from Qt.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.