1

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);
}
smitty_usmc
  • 23
  • 1
  • 1
  • 5

1 Answers1

3
#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

deW1
  • 5,562
  • 10
  • 38
  • 54
  • I am still getting the same errors? Do I need to use the person namespace in any other files? – smitty_usmc Apr 10 '15 at 22:03
  • cpp default constructor added to person.cpp and deleted the duplicate code (duplicate was a mistake in the question not in the program) – smitty_usmc Apr 10 '15 at 22:20
  • @smitty_usmc your mainwindow.h und mainwindow.cpp is still wrong – deW1 Apr 10 '15 at 22:22
  • Can you please be more specific in reference too what is wrong in my mainwindow.h and .cpp files – smitty_usmc Apr 10 '15 at 22:24
  • I fixed the error but now setAge: identifier not found. Please look at the bottom of the question. – smitty_usmc Apr 10 '15 at 22:44
  • @smitty_usmc if you provide your actual mainwindow.h file yes I can look at that – deW1 Apr 10 '15 at 22:55
  • @smitty_usmc fully functional now – deW1 Apr 10 '15 at 23:25
  • you are my savior. Copied your code into the project. Still got errors. Then I ran qmake and it worked. why did running qmake fix the problem? – smitty_usmc Apr 10 '15 at 23:29
  • @smitty_usmc because it recreates the makefile with all dependencies and updates moc / .obj files. after implementing new classes you should do it – deW1 Apr 10 '15 at 23:31
  • i have expanded the program and now i am getting an exact error that won't go away. Can you look at the new code? If yes, respond to this comment and I'll update the code above. – smitty_usmc Apr 11 '15 at 21:26
  • @smitty_usmc check your .cpp again probably forgot the class again for example like you did with `void person::setAge( )` – deW1 Apr 11 '15 at 22:22
  • @smitty_usmc I met the same problem. I tried to remove all the build files and rerun make, it works. Hope it helps. – Jian Guo Jun 01 '17 at 04:44