I am trying to build class Matrix of type template to accept any other type and performing some operations in it.
here is Matrix Class:
#pragma once
#include <iostream>
template<typename T>
class Matrix
{
private:
T ** Mate;
int ROW;
int COL;
void BuildMatrix();
public:
Matrix();
Matrix(int ROW , int COL);
Matrix operator +(Matrix Other);
Matrix operator -(Matrix Other);
Matrix operator *(Matrix Other);
Matrix& operator =(Matrix &Other);
template<typename U>
friend std::istream& operator >>(std::istream& in, Matrix<U>& Obj);
template<typename U>
friend std::ostream& operator << (std::ostream& Out ,const Matrix<U>& Obj);
~Matrix();
};
template<typename T>
Matrix<T>::Matrix()
{
ROW = 0;
COL = 0;
}
template<typename T>
Matrix<T>::Matrix(int ROW, int COL) {
this->ROW = ROW;
this->COL = COL;
this->BuildMatrix();
}
template<typename T>
void Matrix<T>::BuildMatrix() {
Mate = new T*[this->ROW];
for (int i = 0; i < ROW; ++i) {
Mate[i] = new T[this->COL];
}
}
template<typename T>
Matrix<T> Matrix<T>::operator+(Matrix<T> Other){
Matrix tmp;
if (this->ROW != Other.ROW || this->COL != Other.COL) return tmp;
tmp.ROW = Other.ROW;
tmp.COL = Other.COL;
tmp.BuildMatrix();
for (int i = 0; i < tmp.ROW; ++i)
for (int j = 0; j < tmp.COL; ++j)
tmp.Mate[i][j] = this->Mate[i][j] + Other.Mate[i][j];
return tmp;
}
template<typename T>
Matrix<T>& Matrix<T>::operator =(Matrix<T>& Other) {
this->ROW = Other.ROW;
this->COL = Other.COL;
this->BuildMatrix();
for (int i = 0; i < Other.ROW; ++i)
for (int j = 0; j < Other.COL; ++j)
this->Mate[i][j] = Other.Mate[i][j];
return *this;
}
template<typename T>
Matrix<T> Matrix<T>::operator-(Matrix<T> Other){
Matrix tmp;
if (this->ROW != Other.ROW || this->COL != Other.COL) return tmp;
tmp.ROW = Other.ROW;
tmp.COL = Other.COL;
tmp.BuildMatrix();
for (int i = 0; i < tmp.ROW; ++i)
for (int j = 0; j < tmp.COL; ++j)
tmp.Mate[i][j] = this->Mate[i][j] - Other.Mate[i][j];
return tmp;
}
template<typename T>
Matrix<T> Matrix<T>::operator*(Matrix<T> Other){
Matrix tmp;
if (this->COL != Other.ROW) return tmp;
tmp.ROW = this->ROW;
tmp.COL = Other.COL;
tmp.BuildMatrix();
for (int i = 0; i < this->ROW; ++i)
for (int j = 0; j < Other.COL; ++j)
for (int k = 0; k < this->COL; ++k)
tmp.Mate[i][j] = this->Mate[i][k] * Other.Mate[k][j];
return tmp;
}
template<typename T>
std::istream & operator>>(std::istream& in, Matrix<T>& Obj)
{
for (int i = 0; i < Obj.ROW; ++i)
for (int j = 0; j < Obj.COL; ++j)
in >> Obj.Mate[i][j];
return in;
}
template<typename T>
std::ostream & operator<<(std::ostream& Out, const Matrix<T>& Obj)
{
for (int i = 0; i < Obj.ROW; ++i) {
for (int j = 0; j < Obj.COL; ++j) {
Out << Obj.Mate[i][j] << ' ';
}
Out << '\n';
}
return Out;
}
template<typename T>
Matrix<T>::~Matrix()
{
for (int i = 0; i < this->ROW; ++i) {
delete Mate[i];
}
delete[]Mate;
}
The main:
#include <iostream>
#include "Matrix.h"
using namespace std;
int main() {
Matrix<double> Obj(2,2);
cin >> Obj;
cout << Obj << endl;
cout << Obj * Obj << endl;
cout << Obj - Obj << endl;
cout << Obj << endl;
cout << Obj + Obj << endl;
system("pause");
return 0;
}
I/Ostream operator overloading work well but When using + - * operations.the program gives me Run-time error then close the console and i don't why.
So how can i fix it? Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire