I do not know why the error after the definition of the operator overload function.the error is : no match for 'operator=' (operand types are 'Matrix' and 'Matrix'),a1+a4 after operator + overload function is only one matrix object that returns.so here should be a1 = object matrix?
#include<iostream>
using namespace std;
//write your code here
class Matrix{
private:
int arr[3][3];
public:
friend istream& operator >> (istream& in,Matrix &matrix);
friend ostream& operator << (ostream& out,const Matrix& matrix);
friend Matrix operator + (Matrix &a,Matrix &b);
void setArr(int i,int j,int data){
arr[i][j] = data;
}
int getArr(int i,int j){
return arr[i][j];
}
Matrix& operator = (Matrix &b);
};
istream& operator >> (istream& in,Matrix &matrix)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
in >> matrix.arr[i][j];
return in;
}
ostream& operator << (ostream& out,const Matrix &matrix){
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
out << matrix.arr[i][j] << " ";
out << endl;
}
return out;
}
Matrix operator + (Matrix &a,Matrix &b){
Matrix temp;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
temp.setArr(i,j,(a.getArr(i,j)+b.getArr(i,j)));
return temp;
}
Matrix& Matrix::operator = (Matrix &b){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = b.getArr(i,j);
return *this;
}
int main()
{
Matrix a1,a2,a3,a4;
cin >> a1;
cin >> a2;
a4 = a2;
a3 = (a1+a4); //an error over there
cout << a1 << endl;
cout << a2 << endl;
cout << a3 << endl;
return 0;
}