0

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&);

  • the problem is not with the implementation but somewhere with declaration. It looks suspicious that `bool checkInputFile(fstream& inputFile)` is missing the class name. Maybe you wanted `bool Bus::checkInputFile(fstream& inputFile)`. Also please read about [mcve]. If the problem is that the compiler cannot find the function then the implementation isnt the issue, can be removed and you still get the same error – 463035818_is_not_an_ai Jul 29 '22 at 12:10
  • 1
    Looks like you declare `checkInputFile` as member function, but define it as free function. But it's hard to tell without [mcve] – Yksisarvinen Jul 29 '22 at 12:10
  • Yes, I in fact did define it as member function, thank you. I just kept looking at a different line instead bc the error shows up in the function call so completely didn't notice checking the class name in the definition. – Mubasshera Jul 29 '22 at 12:22

0 Answers0