I am trying to refactor a project from github for an assignment. The link to the original project is [https://github.com/nixrajput/bus-reservation-system-cpp.git][1]. Instead of every function in the bus.cpp class opening and checking for input file, I'm trying to replace the repetitive code with a function. One example of a function from original project is:
void Bus::showAllBus()
{
system("cls");
fstream busFileStream;
busFileStream.open("buses.dat", ios::in | ios::app | ios::binary);
if (!busFileStream)
cout << "\n\t\t\t\tFile Not Found...!!!";
else
{
printHeading("BUSES");
busFileStream.read((char *)this, sizeof(*this));
while (!busFileStream.eof())
{
showBusDetails();
busFileStream.read((char *)this, sizeof(*this));
}
busFileStream.close();
}
}
The function I am trying to replace a code chunk with:
bool checkInputFile(fstream& inputFile)
{
inputFile.open("buses.dat", ios::in | ios::app | ios::binary);
if (!inputFile)
{
cout << "\n\t\t\t\t\t\t\t\t\t\tCan't Open File...!!\n";
return false;
}
else
{
return true;
}
}
The function call looks like this:
void Bus::showAllBus()
{
system("cls");
fstream busFileStream;
bool checkFile;
checkFile=checkInputFile(busFileStream);
if (checkFile)
{
printHeading("BUSES");
busFileStream.read((char *)this, sizeof(*this));
while (!busFileStream.eof())
{
showBusDetails();
busFileStream.read((char *)this, sizeof(*this));
}
busFileStream.close();
}
}
I keep getting this error: undefined reference to `Bus::checkInputFile(std::basic_fstream<char, std::char_traits >&)' What am I doing wrong? Is there any other way to have a function that just checks for the input file only. I need the fstream object to be passed by reference since I need it later to read the contents of the file. I cannot have the reading feature in the custom function I am making because I want to call this in other functions as well which have a different flow, so just want to check. My function prototype is alright too: bool checkInputFile(fstream&);