0

I have following piece of code

#include <iostream>
using namespace std;

class TestMatrix {
    int row, col;
    int **arr = NULL;
public:
    friend istream& operator>> (istream &in, TestMatrix &mat);
    friend ostream& operator<< (ostream &out, TestMatrix &mat);

    TestMatrix(int row=1, int col=1):row(row), col(col){
        arr = new int*[row];
        for (int i = 0; i<row; i++) {
            arr[i] = new int[col];
        }
    }
};
TestMatrix() {
    /*cout << "Enter number of rows of your matrix";
    cin >> this->row;
    cout << "Enter number of columns of matrix";
    cin >> this->col;*/
    this->row = 3;
    this->col = 3;
    arr = new int*[this->row];
    for (int i = 0; i<this->row; i++) {
        arr[i] = new int[this->col];
    }
}

istream & operator>> (istream & in, TestMatrix &mat)
{
    cout << "Enter " << mat.row * mat.col << " numbers row wise \n";
    for (int i = 0; i < mat.row; i++) {
        for (int j = 0; j < mat.col; j++) {
            in >> mat.arr[i][j];
        }
    }
    return in;
}

ostream & operator<< (ostream & out, TestMatrix &mat) {
    for (int i = 0; i < mat.row; i++) {
        for (int j = 0; j < mat.col; j++) {
            cout << mat.arr[i][j] << "  ";
        }
        cout << "\n";
    }
    return out;
}

int main() {
    TestMatrix mMatrix1(3, 3);
    TestMatrix mMatrix2();
    //**This works fine
    cin >> mMatrix1;
    //**This gives error
    cin >> mMatrix2;
    return 0;
}

I am trying to overload insertion and extraction operator. I have instantiated class TestMatrix using overloaded constructors. While trying to access overloaded operator with instance which was constructed using constructor without argument, I get error stating binary '>>': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) Can someone please explain reason for same.

leo
  • 35
  • 2
  • 9

0 Answers0