I am trying to learn operator overloading by working on overloading >> for a matrix class to enable the key-board based input for a matrix by calling sth such as
Matrix M1;
cin >> M1;
The operator overloading part is given in the following
istream &operator>>(istream &in, Matrix &m)
{
for (int i = 0; i < m.dx; ++i) {
for (int j = 0; j < m.dy; ++j)
in >> m.p[i][j];
}
return in;
}
It turns work that my implementation was not correct at all. Can you let me know why this implementation is wrong?
I implemented the above part by imitating an existing implementation of overloading >>, which has been proven to work fine in the matrix output part, like cout<< A; where A is a matrix
ostream &operator<<(ostream &out, const Matrix &m)
{
for (int i = 0; i < m.dx; ++i) {
for (int j = 0; j < m.dy; ++j)
out << m.p[i][j] << " ";
out << endl;
}
return out;
}