@seroukhovigor

Как исправить ошибку в xcode signal sigabrt, c++?

Делаю класс матриц, при компиляции вылизает ошибка "Thread 1: signal SIGABRT".
Возникает в диструкторе класса.
Matrix::~Matrix()
{
 for(int i = 0; i < Rows; i++)
   delete []buffer[i];
 delete[] buffer;
};

Весь код:
Matrix.h
#include "stdio.h"

class Matrix
{
private:
  int Rows; // строки матрицы
  int Cols; // столбцы матрицы
  float **buffer; // матрица
public:
  Matrix(int Row = 1, int Col = 1);
  ~Matrix(void);
  
  void Create();
  void Print();
  
  Matrix Sum(Matrix a); // сумма матриц
};


Matrix.cpp

#include "Matrix.h"
#include "cmath"
#include "stdlib.h"
#include "iostream"
#include "iomanip"

using namespace std;

Matrix::Matrix(int Row, int Col)
{
  Rows = Row;
  Cols = Col;
  buffer = new float*[Rows];
  
  for(int j = 0; j < Rows; j++) {
    buffer[j] = new float[Cols];
    for(int i = 0; i < Cols; i++)
      buffer[j][i] = 0.0;
  }
};

Matrix::~Matrix()
{
 for(int i = 0; i < Rows; i++)
   delete []buffer[i];
 delete[] buffer;
};

void Matrix::Create()
{
  int q = 1;
  //cout << "Введите матрицу: " << endl;
  for(int i = 0; i < Rows; i++)
    for(int j = 0; j < Cols; j++) {
      //cout << "[" << i+1 << "]" << "[" << j+1 <<"]: ";
      //cin >> buffer[i][j];
      buffer[i][j] = q;
      q++;
    }
}

void Matrix::Print()
{
  for(int i = 0; i < Rows; i++) {
    for(int j = 0; j < Cols; j++)
      cout << setw(4) << buffer[i][j];
    cout << endl;
  }
}

Matrix Matrix::Sum(Matrix a)
{
  if(Rows == a.Rows && Cols == a.Cols) {
    Matrix t(Rows,Cols);
    for(int i = 0; i < Rows; i++)
      for(int j = 0; j < Cols; j++)
        t.buffer[i][j] = buffer[i][j] + a.buffer[i][j];
    return t;
  } else
    return *this;
}


main.cpp

#include "iostream"
#include "Matrix.h"
using namespace std;
int main(int argc, const char * argv[])
{
  Matrix a(2,2), b(2,2), c(2,2);
  
  a.Create();
  b.Create();
  
  cout << endl;
  cout << "Матрица 1: " << endl;
  a.Print();
  cout << "Матрица 2: " << endl;
  b.Print();
  
  c.Sum(b);
  c.Print();
}


Если есть где-то ошибки, поправьте, пожалуйста.
  • Вопрос задан
  • 2615 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы