0

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;
 }
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by rubberman
1

Have you made the operator >> a friend of the Matrix class? If you don't, it can't get access to the internal parts of Matrix, assuming they are (properly) private or protected. Also, have you determined that the format of the input data is compatible/parseable with the method by which you are reading it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.