Шаблон матрици
Добавлено: 29 июн 2016, 09:05
Код: Выделить всё
#include <iostream>
#include <ctime>
template <typename T>
class Matrix {
int Size;
T** matrix;
void Create(int);
void Delete();
public:
Matrix();
Matrix(int);
~Matrix();
void FillAuto();
void Print();
Matrix operator+(const Matrix &) const;
Matrix operator-(const Matrix &) const;
Matrix operator*(const Matrix &) const;
Matrix operator/(const Matrix &) const;
T Min();
T Max();
};
int main() {
srand((size_t)time(nullptr));
Matrix<int> MI;
Matrix<int> MI1;
Matrix<int> MI2;
MI.FillAuto();
MI1.FillAuto();
MI.Print();
MI1.Print();
MI2 = MI + MI1;
MI2.Print();
MI2 = MI - MI1;
MI2.Print();
MI2 = MI * MI1;
MI2.Print();
std::cout << MI2.Max() << "\n";
std::cout << MI2.Min() << "\n";
return 0;
}
template<typename T>
Matrix<T>::Matrix() {
this->Size = 3;
this->matrix = NULL;
this->Create(this->Size);
}
template<typename T>
Matrix<T>::Matrix(int Size) {
this->Size = Size;
this->matrix = NULL;
this->Create(this->Size);
}
template<typename T>
Matrix<T>::~Matrix() {
this->Delete();
}
template<typename T>
void Matrix<T>::FillAuto() {
for (int i = 0;i < this->Size;i++) {
for (int j = 0;j < this->Size;j++) {
this->matrix[i][j] = rand() % 10;
}
}
}
template<typename T>
void Matrix<T>::Print() {
for (int i = 0; i < this->Size; i++) {
for (int j = 0;j < this->Size;j++) {
std::cout << this->matrix[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\n\n";
}
template<typename T>
Matrix<T> Matrix<T>: :o perator+(const Matrix &Obj) const {
if (this->Size == Obj.Size) {
Matrix<T>Result;
for (int i = 0;i < this->Size;i++) {
for (int j = 0;j < this->Size;j++) {
Result.matrix[i][j] = this->matrix[i][j] + Obj.matrix[i][j];
}
}
return Result;
}
else {
std::cerr << "ERROR!!!\n";
exit(1);
}
}
template<typename T>
Matrix<T> Matrix<T>: :o perator-(const Matrix &Obj) const {
if (this->Size == Obj.Size) {
Matrix<T>Result;
for (int i = 0;i < this->Size;i++) {
for (int j = 0;j < this->Size;j++) {
Result.matrix[i][j] = this->matrix[i][j] - Obj.matrix[i][j];
}
}
return Result;
}
else {
std::cerr << "ERROR!!!\n";
exit(1);
}
}
template<typename T>
Matrix<T> Matrix<T>: :o perator*(const Matrix &Obj) const {
if (this->Size == Obj.Size) {
Matrix<T> Result(this->Size);
for (int i = 0;i < this->Size;i++) {
for (int j = 0;j < this->Size;j++) {
for (int k = 0; k < this->Size; k++) {
Result.matrix[i][j] += this->matrix[i][k] * Obj.matrix[k][j];
}
}
}
return Result;
}
else {
std::cerr << "ERROR!!!\n";
exit(1);
}
}
template<typename T>
Matrix<T> Matrix<T>: :o perator/(const Matrix &) const {
std::cerr << "Error!!! The division of matrix-matrix can not be";
exit(1);
}
template<typename T>
T Matrix<T>::Min() {
T Result = this->matrix[0][0];
for (int i = 0; i < this->Size;i++) {
for (int j = 0; j < this->Size;j++) {
if (Result > this->matrix[i][j]) {
Result = this->matrix[i][j];
}
}
}
return Result;
}
template<typename T>
T Matrix<T>::Max() {
T Result = this->matrix[0][0];
for (int i = 0; i < this->Size;i++) {
for (int j = 0; j < this->Size;j++) {
if (Result < this->matrix[i][j]) {
Result = this->matrix[i][j];
}
}
}
return Result;
}
template<typename T>
void Matrix<T>::Create(int Size) {
if (Size > 1) {
this->Delete();
this->matrix = new T*[Size];
for (int i = 0;i < Size;i++) {
this->matrix[i] = new T[Size];
for (int j = 0;j < Size;j++) {
this->matrix[i][j] = 0;
}
}
}
}
template<typename T>
void Matrix<T>: :D elete() {
if (this->matrix != NULL) {
for (int i = 0;i < Size;i++) {
delete[] this->matrix[i];
}
delete[] this->matrix;
}
}
а теперь вопрос
при вызове деструктора (а точнее метода Delete) выдает ошибку при освобождении памяти