Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I try to write something from a class to a file but it has undefined refrence error c++

I try to write somthing from my class to a file bu it has this error

C:\Users\Lenovo\AppData\Local\Temp\ccaLsCIe.o:main.cpp:(.text+0xe7): undefined reference to `CoronaVaccine::CoronaVaccine(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int, std::__cxx11::basic_string<char, std::char_traits, std::allocator >)’
collect2.exe: error: ld returned 1 exit status

and this is my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

class CoronaVaccine{
    string name;
    string nationalID;
    int dose = 0;
    string nextDate;
    public:
    CoronaVaccine (string="", string="",int=0,string="");
    void setName(string a){
        name = a;
    }
    string getName() const{
        return name;
    }
    void setNatinalID(string b){
        nationalID = b;
    }
    string getNtinalID() const{
        return nationalID;
    }
    void setDoseDate(int c, string d){
        dose = c;
        if (dose == 1){
            nextDate = d;
        }else{
            nextDate = "Done";
        }
        
    }
    int getDose() const{
        return dose;
    }
    string getNextDte() const{
        return nextDate;
    }
};

int main(){
    char cont;
    string nameMain;
    string natinalIdMain;
    int doseMain;
    string nextDateMain;
    CoronaVaccine client;
    ofstream fp("coronaVaccine.txt");
    if (!fp){
        cout << "something goes wrong!";
        exit(0);
    }
    while (1)
    {
        cout << "Name natinalID dose date: \n";
        cin >> nameMain;
        cin >> natinalIdMain;
        cin >> doseMain;
        cin >> nextDateMain;
        client.setName(nameMain);
        client.setNatinalID(natinalIdMain);
        client.setDoseDate(doseMain,nextDateMain);
        cout << "do you want to countinue(y/n): ";
        cin >> cont;
        if (cont == 'n'){
            break;
        }
        fp.write((char *) &client,sizeof(CoronaVaccine));
    }
    cout << "\n==============================\n";
    fp.close();

    return 0;
}

>Solution :

The problem is that you have provided only a declaration for the parameterised constructor CoronaVaccine (string="", string="",int=0,string="");.

You can solve this by providing the corresponding definition as shown below:

//define the constructor. This uses constructor initializer list
CoronaVaccine::CoronaVaccine (string pname, string pnationalID,int pDose,string pnextDate)
            :name(pname), nationalID(pnationalID), dose(pDose), nextDate(pnextDate)
{
    
    
}

So the modified code should look like:

class CoronaVaccine{
    string name;
    string nationalID;
    int dose = 0;
    string nextDate;
    public:
    CoronaVaccine (string="", string="",int=0,string="");//THIS IS A DECLARATION

    //other members here
    
};
//THIS IS THE DEFINITION. This uses constructor initializer list
CoronaVaccine::CoronaVaccine (string pname, string pnationalID,int pDose,string pnextDate)
            :name(pname), nationalID(pnationalID), dose(pDose), nextDate(pnextDate)
{
    
    
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading