Вылетает ошибка при умножении двух матриц
Пытался написать программу, умножающую две матрицы. Но вылетает ошибка, после чего программа завершается. Скрин и код прилагаются. Заранее благодарю!
main.cpp
#include <iostream>
#include "classes.h"
using namespace std;
int main()
{
{
Matrix obj1, obj2;
cout << "Matrix 1: " << obj1 << "\nMatrix 2: " << obj2 << endl;
Matrix result(obj1 * obj2);
cout << "Resulting matrix: " << result << "\nYay!\n`";
obj2 = result;
cout << "After obj2 = result: " << obj2 << "That's it. Bye!\n";
}
return 0;
}
////////////////////
methods.cpp
#include <iostream>
#include "classes.h"
Matrix::Matrix()
{
std::cout << "\nEnter the size of the matrix.\nEnter rows: ";
std::cin >> rows;
std::cout << "Enter columns: ";
std::cin >> cols;
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
}
for (int i = 0; i < rows; i++)
{
std::cout << "\nRow #" << i + 1 << ":\n";
for (int j = 0; j < cols; j++)
{
std::cout << " column #" << j + 1 << ": ";
std::cin >> matrix[rows][cols];
}
}
}
Matrix::Matrix(int _rows, int _cols)
{
rows = _rows;
cols = _cols;
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
for (int j = 0; j < cols; j++)
{
matrix[i][j] = 0;
}
}
}
Matrix::Matrix(const Matrix& obj)
{
rows = obj.rows;
cols = obj.cols;
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[rows][cols] = obj.matrix[rows][cols];
}
}
}
Matrix::~Matrix()
{
std::cout << "\nMatrix deleted.\n";
for (int i = 0; i < rows; i++)
{
delete[] matrix[i];
}
delete[] matrix;
matrix = nullptr;
}
Matrix Matrix::operator=(const Matrix& obj)
{
if (this == &obj)
return *this;
this->~Matrix();
rows = obj.rows;
cols = obj.cols;
matrix = new double* [rows];
for (int i = 0; i < rows; i++)
{
matrix[i] = new double[cols];
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[rows][cols] = obj.matrix[rows][cols];
}
}
return *this;
}
Matrix operator*(const Matrix& obj1, const Matrix& obj2)
{
Matrix temp(obj1.rows, obj2.cols);
if (obj1.cols != obj2.rows)
{
std:: cout << "\nThe matrix multiplication is impossible due to a rows and cols dismatch. \nPlease, enter another matrix sizes.\n\n";
return obj1;
}
for (int i = 0; i < obj1.rows; i++)
{
for (int j = 0; j < obj2.cols; j++)
{
for (int k = 0; k < obj1.cols; k++)
{
temp.matrix[i][j] += obj1.matrix[i][k] * obj2.matrix[k][j];
}
}
}
return temp;
}
std::ostream& operator<<(std::ostream& os, const Matrix& obj)
{
std::cout << "\n\n";
for (int i = 0; i < obj.rows; i++)
{
std::cout << " | ";
for (int j = 0; j < obj.cols; j++)
{
std::cout << obj.matrix[i][j] << " ";
}
std::cout << "\b| \n";
}
return os;
}
/////////////////
classes.h
#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
class Matrix
{
private:
double** matrix;
int rows, cols;
public:
Matrix();
Matrix(int _rows, int _cols);
Matrix(const Matrix& obj);
~Matrix();
Matrix operator=(const Matrix& obj);
friend Matrix operator*(const Matrix& obj1, const Matrix& obj2);
friend std::ostream& operator<<(std::ostream& os, const Matrix& obj);
};
#endif CLASSES_H
