Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does fstream not utilize the istream prototype of operator>>?

I have a class that uses a friend function to overload the operator>>. The overloaded operator method tests well on standard cin use. However, when I try to upgrade the code to use ifstream objects instead of istream objects, the prototype is not recognized as a valid method.

It is my understanding that ifstream is inherited from istream, and as such, polymorphism should allow ifstream objects to operate with the istream overloaded function. What is wrong with my understanding?

Is it necessary to duplicate the function for every input stream type?

Class:

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

using namespace std;

class Hospital {
public:
    Hospital(std::string name);
    std::string getName();
    void write();
    friend ostream & operator<<( ostream &os, Hospital &hospital );
    friend istream & operator>>( istream &is, Hospital &hospital );
private:
    void readFromFile( std::string filename );
    std::string m_name;
};

function implementation:

istream &operator>>( istream &is, Hospital &hospital ){
    getline( is, hospital.m_name );
    return is;
}

Error:

Hospital.cpp: In member function ‘void Hospital::readFromFile(std::string)’: Hospital.cpp:42:24: error: no match for ‘operator>>’ (operand types are ‘std::ifstream {aka std::basic_ifstream}’ and ‘Hospital*’) storedDataFile >> this;

This error occurs in the stack after a call to readFromFile, which I copy here for completeness:

/**
 * A loader method that checks to see if a file exists for the given file name.
 * If no file exists, it exits without error. If a file exists, it is loaded
 * and fills the object with the contained data. WARNING: This method will overwrite
 * all pre-existing and preset values, so make changes to the class only after
 * invoking this method. Use the write() class method to write the data to a file.
 * @param filename
 */
void Hospital::readFromFile(std::string filename) {
    ifstream storedDataFile( filename.c_str() );
    if( storedDataFile ){
        storedDataFile >> this;
        storedDataFile.close();
    }
}

In this situation, 'this' is a Hospital object.

All help and ideas are appreciated. I am reteaching myself C++ and searching for a deeper understanding of the language and its processes.

like image 643
Jared Clemence Avatar asked Sep 07 '15 22:09

Jared Clemence


People also ask

Does ifstream inherit from istream?

File streams come in two flavors also: the class ifstream (input file stream) inherits from istream, and the class ofstream (output file stream) inherits from ostream. Thus all of the member functions and operators that you can apply to an istream or ostream object can also be applied to ifstream and ofstream objects.

Does Fstream include Iostream?

An fstream is an iostream which writes to and reads from a file. So: every fstream is an iostream but not every iostream is an fstream.


1 Answers

You'll have to use :

storedDataFile >> *this;
               // ~~ dereference the `this` pointer (i.e. Hostipal Object)
              /* Enabling the match for operator>>( istream &is, Hospital &hospital ) */
like image 104
P0W Avatar answered Sep 28 '22 15:09

P0W