error: LNK2019: unresolved external symbol



  • I am brand new to Qt so I am learning it by following a video called "Learning Qt 5". But I soon encountered a problem. The project is as follows.

    ============ main.cpp ===============

    #include <QCoreApplication>
    #include <QtDebug>
    #include "myparentclass.h"
    #include "mychild.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        MyParentClass * parent= new MyParentClass();
        MyChild * child=new MyChild(parent);
    
        qDebug()<<"Hello World!\n";
    
        delete parent;
        delete child;
    
        return a.exec();
    }
    

    ================= myparentclass.h ===================

    #ifndef MYPARENTCLASS_H
    #define MYPARENTCLASS_H
    
    #include <QObject>
    
    class MyParentClass : public QObject
    {
        Q_OBJECT
    public:
        explicit MyParentClass(QObject *parent = 0);
        ~MyParentClass();
    
    signals:
    
    public slots:
    };
    
    #endif // MYPARENTCLASS_H
    

    ================= myparentclass.cpp ====================

    #include "myparentclass.h"
    #include <QCoreApplication>
    
    MyParentClass::MyParentClass(QObject *parent) : QObject(parent)
    {
    
    }
    
    MyParentClass::~MyParentClass()
    {
        qDebug("MyParentClass destructor");
        QCoreApplication::quit();
    }
    

    ================== mychild.h ========================

    #ifndef MYCHILD_H
    #define MYCHILD_H
    
    #include <QObject>
    
    class MyChild : public QObject
    {
        Q_OBJECT
    public:
        explicit MyChild(QObject *parent = 0);
        ~MyChild();
    
    signals:
    
    public slots:
    };
    
    #endif // MYCHILD_H
    

    ==================== mychild.cpp =====================

    #include "mychild.h"
    
    MyChild::MyChild(QObject *parent) : QObject(parent)
    {
    }
    
    MyChild::~MyChild()
    {
        qDebug("MyChild destructor");
        this->parent()->deleteLater();
    }
    

    I have added all these files into the Qt project:
    alt text

    But when I build it, I got errors:
    alt text

    I searched some previous threads which suggest clean and rebuild, but it does not work.

    The Qt I am using is 5.10.0 and the Qt Creator is 4.5.0. The OS is Windows 7. I have Visual Studio 2015 installed so I want to use VS to compile. This is the kit configuration:
    alt text

    So, what am I missing? How to fix the errors? Thank you.


  • Qt Champions 2016

    @zzzhhhzzzhhh
    Hi
    Code looks correct and it seems to complain about the construtor.

    Normally, it can be resolved by
    deleting the build folder completely and then rebuild all.

    You can test your Kits by making a default GUI project and see if that runs.



  • Hi @zzzhhhzzzhhh

    adding few words, in main function "delete child" is not required. As parent "parent" will take of deleting child.
    Parent Child relationship in Qt


  • Qt Champions 2016

    Hi
    To expand on @yuvaram good catch,
    i consider this a "must read" for c++ programmers new
    to Qt.
    http://doc.qt.io/qt-5/objecttrees.html



  • Thank you all so much! After I deleted the folder "build-HelloWorld-Desktop_Qt_5_10_0_MSVC2015_64bit-Debug" and rebuild the project, all linking errors are gone. Also, I will read the links you gave me and pay more attention to this issue later. Thank you again!


Log in to reply
 

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