lundi 29 juin 2015

How to fix error: LNK2019 in c++?

I have 3 header file and 4 source file:

calculator.h

#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QMainWindow>


namespace Ui {
class Calculator;
}

class Calculator : public QMainWindow
{
    Q_OBJECT

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





private slots:
    //numbers

    void ClickZero();
    void ClickOne();
    void ClickTwo();
    void ClickThree();
    void ClickFour();
    void ClickFive();
    void ClickSix();
    void ClickSeven();
    void ClickEight();
    void ClickNine();



    void ClickDot();
    void ClickEquel();

    void showMessage(std::string msg);



private:
    Ui::Calculator *ui;
};

#endif // CALCULATOR_H

parser.h

#include "calculator.h"
#include "ui_calculator.h"
#include <iostream>
#include <sstream>
#include <string>



using namespace std;
class ParserFormula{

    public:
         void showMessage(string msg);
          string* stringToArray(string str);

};

task.h

#include "calculator.h"
#include "ui_calculator.h"
#include <iostream>
#include <sstream>
#include <string>



using namespace std;
class Task{

    public:
         void showMessage(string msg);
          string* stringToArray(string str);

};

calculator.cpp

#include "calculator.h"
#include "ui_calculator.h"
#include <iostream>
#include <sstream>
#include <string>
#include <parser.h>
#include <task.h>


#include <QMessageBox>

using namespace std;

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





     connect(ui->btn0, SIGNAL(clicked()), this, SLOT( ClickZero()));
     connect(ui->btn1, SIGNAL(clicked()), this, SLOT( ClickOne()));
     connect(ui->btn2, SIGNAL(clicked()), this, SLOT( ClickTwo()));
     connect(ui->btn3, SIGNAL(clicked()), this, SLOT( ClickThree()));
     connect(ui->btn4, SIGNAL(clicked()), this, SLOT( ClickFour()));
     connect(ui->btn5, SIGNAL(clicked()), this, SLOT( ClickFive()));
     connect(ui->btn6, SIGNAL(clicked()), this, SLOT( ClickSix()));
     connect(ui->btn7, SIGNAL(clicked()), this, SLOT( ClickSeven()));
     connect(ui->btn8, SIGNAL(clicked()), this, SLOT( ClickEight()));
     connect(ui->btn9, SIGNAL(clicked()), this, SLOT( ClickNine()));



     connect(ui->btnDot, SIGNAL(clicked()), this, SLOT( ClickDot()));
     connect(ui->btnEql, SIGNAL(clicked()), this, SLOT( ClickEquel()));
}

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


void Calculator::ClickZero(){

    ui->mainTextBox->setText( ui->mainTextBox->text() + "0");

}

void Calculator::ClickOne(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "1");
}
void Calculator::ClickTwo(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "2");
}
void Calculator::ClickThree(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "3");
}
void Calculator::ClickFour(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "4");
}
void Calculator::ClickFive(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "5");
}
void Calculator::ClickSix(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "6");
}
void Calculator::ClickSeven(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "7");
}
void Calculator::ClickEight(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "8");
}
void Calculator::ClickNine(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + "9");
}


void Calculator::ClickDot(){
    ui->mainTextBox->setText( ui->mainTextBox->text() + ".");
}

void Calculator::ClickEquel(){

        ParserFormula Parser;




   string CurentText = ui->mainTextBox->text().toStdString();

   Parser.showMessage("ss");



  //string* charArray = Parser.stringToArray(CurentText);


//    size_t num = 0;
//    string val = "";




//    for(;num<CurentText.length();num++){

//        val += charArray[num];


//    }

//    this->showMessage(val);





//    for(int c = 0;c<sizeof(charArray);c++){

//        //
//        cout<<charArray[c];
//        cout<<"|";
//        cout<<c;
//        cout<<"\n";

//    }





}


void Calculator::showMessage(string msg){

    QMessageBox msgBox;
    msgBox.setText(QString::fromStdString(msg));
    msgBox.exec();

}



**main.cpp**

    #include "calculator.h"
#include <QApplication>

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

    return a.exec();
}

parser.cpp

 #include "Parser.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <QMessageBox>




void ParserFormula::showMessage(string msg){

    QMessageBox msgBox;
    msgBox.setText(QString::fromStdString(msg));
    msgBox.exec();

}

string* ParserFormula::stringToArray(string str){


size_t size = str.size();
string *returnArray = new string[size];



for(size_t  c = 0;  str.length()>c;    c++){

    returnArray[c] = str.substr(c,1);



}

return returnArray;

}

task.cpp

#include "task.h"
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <QMessageBox>




void Task::showMessage(string msg){

    QMessageBox msgBox;
    msgBox.setText(QString::fromStdString(msg));
    msgBox.exec();

}

string* Task::stringToArray(string str){


    size_t size = str.size();
    string *returnArray = new string[size];



    for(size_t  c = 0;  str.length()>c;    c++){

        returnArray[c] = str.substr(c,1);



    }

    return returnArray;

}

and when i compile it getting this error

calculator.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall ParserFormula::test(class std::basic_string,class std::allocator >)" (?test@ParserFormula@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced

Task and ParserFormula class are both same, only difference their names. So when write Task Parser; instead of ParserFormula Parser; it run perfect. what am I doing wrong ?

Aucun commentaire:

Enregistrer un commentaire