S
S
seroukhovigor2014-05-12 11:48:29
Apple Xcode
seroukhovigor, 2014-05-12 11:48:29

How to fix error in xcode signal sigabrt, c++?

I'm making a class of matrices, while compiling I get the error "Thread 1: signal SIGABRT".
Occurs in the class destructor.

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

All code:
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();
}

If there are errors somewhere, please correct them.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question