2015-09-07 15 views
5

Mam klasy, która używa funkcji znajomego do przeciążenia operatora >>. Przeciążona metoda operatora sprawdza się dobrze w standardowym użyciu cin. Jednak, gdy próbuję uaktualnić kod, aby użyć obiektów typu "onstream" zamiast obiektów istream, prototyp nie zostanie rozpoznany jako poprawna metoda.Dlaczego fstream nie wykorzystuje prototypu operatora istream >>?

Rozumiem, że ifstream jest dziedziczone z istream, i jako takie, polimorfizm powinien umożliwiać obiektom zewnętrznym działanie z przeładowaną funkcją istream. Co jest nie tak z moim zrozumieniem?

Czy konieczne jest zduplikowanie funkcji dla każdego typu strumienia wejściowego?

Klasa:

#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; 
}; 

realizacja funkcji:

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

Błąd:

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;

Ten błąd występuje w stosie po wywołaniu readFromFile, który kopiuję tutaj dla kompletności :

/** 
* 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(); 
    } 
} 

W tej sytuacji "to" jest obiektem szpitala.

Cała pomoc i pomysły są doceniane. Odpowiadam sobie na C++ i szukam głębszego zrozumienia języka i jego procesów.

+0

'this' jest * wskaźnik do obiektu' * a Hospital'. –

+0

Podaj nazwę przez odniesienie do const i możesz również zwrócić nazwę z 'getName' przez odwołanie do const, jeśli jest to tylko element klasy. 'operator <<' powinien pobrać Hospital by const reference. –

Odpowiedz

8

Będziesz musiał użyć:

storedDataFile >> *this; 
       // ~~ dereference the `this` pointer (i.e. Hostipal Object) 
       /* Enabling the match for operator>>(istream &is, Hospital &hospital) */ 
+0

Genialny! Całkowicie zapomniałem, że operator "-> automatycznie usuwa to, więc bez niego muszę ręcznie usunąć dereferencję! Dziękuję bardzo! Mam limit czasowy, zanim mogę przyjąć twoją odpowiedź, wrócę po upłynięciu limitu, aby przyznać ci punkty. Jeszcze raz dziękuję! –

Powiązane problemy