How to overload the ostream operator (<<) so that it also allows printing the result of another overloaded operator (say * or +)

49 views Asked by At

I have created a class called Matrices which implements basic matrix operations. I have overloaded the +,-,* >>,<< operators so that I can perform the usual operations on matrix objects. However, if I try to print the result of A+B using the << operator, it gives an error of no matching arguments found. Here is my complete code:

#include<iostream>
using namespace std;
class Matrix {
    long MaxRow;
    long MaxCol;
    double** p;
public:
    Matrix()
    {
        MaxRow = MaxCol = 0;
        p = NULL;
    }
    Matrix(long r, long c);
    Matrix operator + (Matrix B);
    Matrix operator - (Matrix B);
    Matrix operator * (Matrix B);
    friend ostream& operator <<(ostream& out,Matrix &B);
    friend istream& operator >>(istream& in,Matrix &A);
};
Matrix::Matrix(long r, long c)
{
    MaxRow = r;
    MaxCol = c;
    p = new double* [MaxRow];
    for (long j = 0; j < MaxRow; j++)
        p[j] = new double[MaxCol];
}
Matrix Matrix:: operator + (Matrix B)
{
    if (MaxRow != B.MaxRow || MaxCol != B.MaxCol)
    {
        cout << "Given matrices cannot be added";
        return *this;
    }
    else
    {
        Matrix C(MaxRow,MaxCol);
        for (long i = 0; i < MaxRow; i++)
            for (long j = 0; j < MaxCol; j++)
                C.p[i][j] = p[i][j] + B.p[i][j];
        return C;
    }

}
Matrix Matrix:: operator - (Matrix B)
{
    if (MaxRow != B.MaxRow || MaxCol != B.MaxCol)
    {
        cout << "Given matrices cannot be subtracted";
        return *this;
    }
    else
    {
        Matrix C(MaxRow,MaxCol);
        for (long i = 0; i < MaxRow; i++)
            for (long j = 0; j < MaxCol; j++)
                C.p[i][j] = p[i][j] - B.p[i][j];
        return C;
    }

}
Matrix Matrix:: operator * (Matrix B)
{
    if (MaxCol != B.MaxRow)
    {
        cout << "Given matrices cannot be multiplied";
        return *this;
    }
    else
    {
        Matrix C(MaxRow,B.MaxCol);
        for (long i = 0; i < MaxRow; i++)
        {
            for (long j = 0; j < B.MaxCol; j++)
            {
                double sum = 0;
                for (long k = 0; k < MaxCol; k++)
                    sum += p[i][k] * B.p[k][j];
                C.p[i][j] = sum;
            }
        }
        return C;
    }

}
ostream& operator<<(ostream& out,Matrix&B)
{
    for (long i = 0; i < B.MaxRow; i++)
    {
        out << endl;
        for (long j = 0; j < B.MaxCol; j++)
            out << B.p[i][j] << " ";

    }
    return out;
}
istream& operator>> (istream& in, Matrix &A)
{
    for (long i = 0; i < A.MaxRow; i++)
        for (long j = 0; j < A.MaxCol; j++)
            in >> A.p[i][j];
    return in;
}
int main()
{
    Matrix A(3, 3), B(3, 2);
    cin >> A;
    cin >> B;
    cout << "\nThe product of A and B is: " << A*B;
        return 0;


}

The exact error is: C++ no operator matches these operands operand types are: std::basic_ostream<char, std::char_traits<char>> << Matrix.

It works fine if I change main() the code like this:

int main()
{
    Matrix A(3, 3), B(3, 2);
    cin >> A;
    cin >> B;
        Matrix C=A*B
    cout << "\nThe product of A and B is: " << C;
        return 0;


}

Since the result of A*B is also a matrix object, I expected the << operator to work normally.

0

There are 0 answers