Problem z operatorem wejścia

0

Witam forumowiczów

Mam problem z poniższym kodem tzn. pętla wejściowa nie dział tak jakbym sobie tego życzył. Jak dopiszę w pętli while(ist >> r.n) to wszystko jest tip top ale w takim razie po co definiować operator wejścia ?
PROSZĘ O WAS O JAKIEŚ RADY :)

#include <iostream>
#include <fstream>
#include <cctype>
#include <vector>
#include <string>
#include <conio.h>

using namespace std;

struct Reading
{
	int n;
	Reading(int u):n(u) {}
	~Reading() {}
};

istream& operator>>(istream& is, Reading r)
{
	int nn;
	is >> nn;
	r.n = nn;
	return is;
}



ostream& operator<<(ostream& os, Reading& r)
{
	return os << r.n;
}



int main()
{
	Reading rr(0);
	vector<Reading> r;

	cout << ">>";
	string name;
	cin >> name;

	ofstream ost(name.c_str());

	for(int i = 0; i<5; i++)
	{
		rr.n = i;
		r.push_back(rr);
	}
	
	for(int i = 0; i<r.size(); i++)
		ost << r[i] << endl;

	r.clear();

	cout << "\n>>";
	string iname;
	cin >> iname;
	ifstream ist(iname.c_str());

	while(ist >> rr) 
		{
			r.push_back(rr);
			if(ist.eof()) break;
	}

	for(int i = 0; i<r.size(); i++)
	{
		cout << r[i].n;
	}

	getch();
	return 0;
}
0

Po pierwsze nie deklaruj destruktora, jak Ci nie jest potrzebne (tzn. jest pusty). A kod nie działa, bo do przeciążenia operatora >> podajesz kopie obiektu klasy reading. Dodatkowo przy operatorze << powinieneś podawać albo kopię, albo stałą referencję.

1 użytkowników online, w tym zalogowanych: 0, gości: 1