Przeciążanie operatora wpisywania

0
 class student
{

private:
	string imie;
	string nazwisko;
	long nr_indeks;
public:
	long index();
	student();
	student(string imie,string nazwisko,long nr_indeks);
	friend ostream& operator<< (ostream& stream, student& st);
	friend bool operator==(long &indeks, student &st);
	friend istream& operator >> (istream& stream, student &st);
};


ostream& operator<<(ostream& stream, student& st) {
	stream << st.imie << " " << st.nazwisko << " " << st.nr_indeks<<endl;
	return stream;
}

bool operator==(long & indeks, student & st)
{
	if (indeks == st.index()) {
		return true;
	}
	else return false;
}
istream & operator >> (istream & stream, student & st)
{
		stream >> st.imie >> st.nazwisko >> st.nr_indeks;
		return stream;
}
long student::index()
{
	return nr_indeks;
}
student::student(){
	imie = "Panx";
	nazwisko="Ixinski";
	nr_indeks = 0;

}
student::student(string imie, string nazwisko, long nr_indeks) :
imie(imie), nazwisko(nazwisko), nr_indeks(nr_indeks) {	}

int main(){
student b;// działa 
student b();// nie działa i nie wiem dlaczego
	cin >> b;
}

Nie rozumiem dlaczego gdy piszę student b() to wtedy mam błąd "no operator ">>" matches these operands" a jak piszę student b; to wtedy wszystko działa. Czym się różnią te zapisy?
Czy to dlatego że nie dałoby się odróżnić tej definicji zmiennej od deklaracji funkcji bezparametrowej? i ja zapisałem to niejednoznacznie?

2

Drugi przypadek to deklaracja funkcji o nazwie b, która zwraca obiekt typu student i nie przyjmuje żadnych parametrów. Po więcej szczegółów googluj "C++ the most vexing parse".

0

Przydałoby się const-correctness:

    long index() const;
    friend ostream& operator<<(ostream &stream, const student &st);
    friend bool operator==(long indeks, const student &st);

bo tak jak jest, to nie mógłbyś wyświetlić obiektu typu const student, ani wywołać na nim index().

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