Search code examples
c++qtlnk2019

Error LNK2019: unresolved external symbol C++ in Qt platform


I'm getting these three errors and I have no idea why? I am using the Qt program and and the code below goes in a Qt widget project. The mainwindow.ui is not included because I can't change it anyways. The goal as of now is to get the age into a lineEdit as shown in the mainwindow.cpp file.

I have done things like uninstalled and reinstalled Qt program. Copy and pasted code into a brand new project. Added the header files to cpp files etc etc

Errors:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall person::~person(void)" (??1person@@QAE@XZ) referenced in function "public: virtual __thiscall MainWindow::~MainWindow(void)" (??1MainWindow@@UAE@XZ)

and

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall person::person(void)" (??0person@@QAE@XZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)

and

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall person::setAge(int)" (?setAge@person@@QAEXH@Z) referenced in function "private: void __thiscall MainWindow::on_lineEdit_returnPressed(void)" (?on_lineEdit_returnPressed@MainWindow@@AAEXXZ)

Here is the code:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <string>
#include "person.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

 public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_lineEdit_returnPressed();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

person.h

#ifndef PERSON_H
#define PERSON_H


class person
{
public:

    int age;

    person();
    person(int);

    ~person();

    int getAge();

    void setAge(int age);
};

#endif // PERSON_H

main.cpp

#include "mainwindow.h"
#include "person.h"
#include <QApplication>
#include <string>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "person.h"
#include <string>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_lineEdit_returnPressed()
{
    QString str = ui->lineEdit->text();
    std::string age = str.toStdString();
    x.setAge(stoi(age));
    ui->lineEdit_2->setText(str);
}

person.cpp

#include "person.h"
using namespace std;

person::person(int age)
{
    age = 0;
}

person::~person()
{

}

int getAge()
{
    return age;
}

void setAge(int age)
{ 
    age = newAge;
}

I have searched and searched and all I know is for some reason the program can't find the setAge() method or cant find the person class. I've even asked other people who know c++ and have no idea why this is happening. Please help!

Deleting:

person x; //in the mainwindow.h file solves this problem but...

I get the error:

C:\Users\Adam\Documents\Body_fat\mainwindow.cpp:22: error: C3861: 'setAge': identifier not found



void MainWindow::on_lineEdit_returnPressed()
{
    QString str = ui->lineEdit->text();
    std::string age = str.toStdString();
    setAge(stoi(age));
    ui->lineEdit_2->setText(str);
}

Solution

  • #include "person.h"
    using namespace std;
    
    person::person(int age)
    {
        age = 0;
    }
    
    person::~person()
    {
    
    }
    
    int person::getAge() // <--
    {
        return age;
    }
    
    void person::setAge(int age) // <-- 
    { 
        age = newAge;
    }
    

    You're in the class so you have to use the namespace and yes that error is somewhat irritating.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    

    This needs to be corrected as well. Also your mainwindow.h and ui_mainwindow.h seem to have the same code which is wrong as well.

    Your person.cpp is also missing

    person::person( )
    {
    
    }
    

    main.cpp doesn't need string and person.h so no need to include that there.


    Full working code below:


    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "person.h"
    
    namespace Ui {
        class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
            Q_OBJECT
    
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
    
        private slots:
            void on_lineEdit_returnPressed();
    
        private:
            Ui::MainWindow *ui;
            person p;
    };
    
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_lineEdit_returnPressed()
    {
        QString str     = ui->lineEdit->text();
        std::string age = str.toStdString();
    
        p.setAge( stoi( age ) );
        ui->lineEdit_2->setText( str );
    }
    

    person.h

    #ifndef PERSON_H
    #define PERSON_H
    
    class person
    {
        public:
    
            int age;
    
            person();
            person(int);
    
            ~person();
    
            int getAge();
    
            void setAge(int age);
    };
    
    
    #endif // PERSON_H
    

    person.cpp

    #include "person.h"
    
    person::person( )
    {
    
    }
    
    person::person(int age)
    {
        age = 0;
    }
    
    person::~person()
    {
    
    }
    
    int person::getAge()
    {
        return age;
    }
    
    void person::setAge(int age)
    {
        this->age = age;
    }
    

    If that gives you the errors again Run Qmake