Reputation:
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
#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;
}
Upvotes: 0
Views: 169
Reputation: 193
You must define the body of the constructor. String is by default initialized to be empty, so no need to explicitly add ="".
Constructor signature should match the declaration, definition and usage.
CoronaVaccine (const string &n, const string &id, const int &d,const string &date){
...
}
https://www.developerfusion.com/article/133063/constructors-in-c11/
Upvotes: 0
Reputation: 1
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)
{
}
Upvotes: 0
Reputation: 4747
You only declare, but don't define the constructor:
CoronaVaccine (string="", string="",int=0,string="");
You have to define it (i.e. giving it a body)
Upvotes: 1