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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.