我有以下代码
#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;
}
我正在尝试重载插入和提取操作符。 我使用重载的构造函数实例化了类TestMatrix。 在尝试使用不带参数的构造函数构造的实例访问重载运算符时,我得到错误说明 二进制&#39;&gt;&gt;&#39;:找不到哪个运算符采用类型&#39;重载函数&#39;的右手操作数。 (或者没有可接受的转换) 有人可以解释原因。