I just want to overload operator with matrix, but I have a problem when I use it. The program can get through operator function but have a problem with this line tong = mt1+mt2
the same problem with line tich = mt1*mt2
. I can't understand why. Please help me. Thanks you so much for your help.
#include <stdio.h>
#include <conio.h>
#include <iostream>
using namespace std;
class MT{
private:
int row;
int col;
int **matrix;
public:
MT(){
row = 0;
col = 0;
matrix = NULL;
}
MT(int row1, int col1){
row = row1;
col = col1;
matrix = new int *[row];
for (int i = 0; i<row; i++)
matrix[i] = new int[col];
}
~MT(){
for (int i = 0; i<row; i++)
delete []matrix[i];
delete []matrix;
}
friend ostream& operator<< (ostream &os,const MT &mt){
for (int i = 0; i<mt.row; i++){
for (int j = 0; j<mt.col; j++)
cout << mt.matrix[i][j]<<" ";
cout <<endl;
}
return os;
}
friend istream& operator>> (istream &is,MT &mt){
int row1, col1;
cout <<"\n Nhap so row ma tran: "; fflush(stdin); cin >> row1;
cout <<"\n Nhap so col ma tran: "; fflush(stdin); cin >> col1;
mt.row = row1;
mt.col = col1;
mt.matrix = new int *[row1];
for (int i = 0; i<row1; i++)
mt.matrix[i] = new int[col1];
for (int i = 0; i < row1; i++)
for (int j = 0; j< col1; j++)
is >> mt.matrix[i][j];
return is;
}
MT operator+(MT &mt){
MT temp(mt.row,mt.col);
if ((this->row != mt.row) || (this->col != mt.col)){
cout <<"\n Khong the thuc hien phep cong!";
return temp;
}
else{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
temp.matrix[i][j] = this->matrix[i][j] + mt.matrix[i][j];
return temp;
}
}
MT operator-(MT &mt){
MT temp(mt.row,mt.col);
if ((this->row != mt.row) || (this->col != mt.col)){
cout <<"\n Khong the thuc hien phep tru!";
return temp;
}
else{
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
temp.matrix[i][j] = this->matrix[i][j] - mt.matrix[i][j];
return temp;
}
}
MT operator*(MT &mt){
MT temp(this->row, mt.col);
if (this->row != mt.col){
cout <<"\n Khong the thuc hien phep nhan.";
return temp;
}
else{
for (int i = 0; i < this->row; i++)
for (int j = 0; j < mt.col; j++)
{
temp.matrix[i][j] = 0;
for (int k = 0; k<this->row; i++)
temp.matrix[i][j] += this->matrix[i][k]*mt.matrix[k][j];
}
return temp;
}
}
MT operator=(MT &mt){
row = mt.row;
col = mt.col;
this->matrix = new int *[row];
for (int i=0; i<row; i++)
this->matrix[i] = new int[col];
for (int i =0; i<row; i++)
for (int j = 0; j<col; j++)
this->matrix[i][j] = mt.matrix[i][j];
return mt;
}
};
void main()
{
MT mt1,mt2,tong,tich;
cin>>mt1;
cin>>mt2;
tong = mt1+mt2;
cout <<tong;
tich = mt1*mt2;
cout <<tich;
_getch();
}