Hello. I am new to programming and am just starting to learn a lot (although I have some training, it is funny to consider it at least serious). Seeking classes, trying to build something to work with matrices. The implementation code is not important, it is important that I get the error: http://prntscr.com/e6y5on The key question is how to fix it.
Matrix.h:
#ifndef MATRIX_H #define MATRIX_H class Matrix { private: int m_rows = 1; int m_cols = 1; int** m_matrix = new int* [m_rows]; public: Matrix(int rows, int cols); void SetSize(int rows, int cols); void Create(); int rows(); int cols(); ~Matrix(); }; #endif Matrix.cpp:
#include <iostream> #include <math.h> #include "matrix.h" using namespace std; Matrix::Matrix(int rows, int cols) { SetSize(rows, cols); Create(); } void Matrix::SetSize(int rows, int cols) { m_rows = rows; m_cols = cols; } void Matrix::Create() { for (int i = 0; i < m_rows; i++) m_matrix[i] = new int[m_cols]; cout << "Matrix created with " << m_rows << " rows and " << m_cols << " cols." << endl; } int Matrix::rows() { return m_rows; } int Matrix::cols() { return m_cols; } Matrix::~Matrix() { for (int i = 0; i < m_rows; i++) delete[] m_matrix[i]; delete[] m_matrix; cout << "Matrix deleted" << endl; } main.cpp:
int main() { Matrix m(2,2); return 0; } When step-by-step checking in Visual Studio, the program is poured upon the removal of the external pointer delete [] m_matrix; Actually, please help =)