0

This is supposed to be my HW in OOP course. SO i asked to create matrix class. Code works fine - all methods and new operands works fine BUT when my D`tor is empty and when i write there code to free memory i get this error. Thanks for help

int main()
{
MyMatrix m1(3, 3);
MyMatrix m2(3, 3);
MyMatrix res(3, 3);

m1.Set();
m2.Set();

cout << m1;
cout << m2;

res = m1 + m2;

cout << res;

}

CPP

MyMatrix::MyMatrix(int row, int col) // C`tor with specific data
{
n = row;
m = col;

matrix = new int* [n]; // Memory  allocation for rows

for (int i = 0; i < n; i++)
{
    matrix[i] = new int[m]; // Memory Allocation for columns
}


for (int i = 0; i < n; i++)
{

    for (int j = 0; j < m; j++)
    {

        matrix[i][j] = 0;

    }
}
}

MyMatrix::~MyMatrix() // Default d`tor 
{

for (int i = 0; i < n; i++)
{
    delete[] matrix[i];
}
delete[] matrix;
}

void MyMatrix::Set()
{
cout << "Enter new row" << endl;
for (int i = 0; i < n; i++)
{

    for (int j = 0; j < m; j++)
    {

        cin >> matrix[i][j];

    }

    if (i != (n - 1))
    {
        cout << "Enter new row" << endl;
    }

  }
}


ostream& operator<<(ostream& out, const MyMatrix& matrix)
{

for (int i = 0; i < matrix.n; i++)
{

    //run in loop on every column.
    for (int j = 0; j < matrix.m; j++)
        //print value with a space.
        out << matrix.matrix[i][j] << "t";
    //at the end of every row jump line.
    out << endl;
}

out << endl;
return out;
}


MyMatrix& MyMatrix::operator= (const MyMatrix& mat1)
{
n = mat1.n;
m = mat1.m;

for (int i = 0; i < n; i++)
{

    for (int j = 0; j < m; j++)
    {

        matrix[i][j] = mat1.matrix[i][j];

    }

}

return *this;

}

const MyMatrix MyMatrix::operator+(const MyMatrix& mat1) const
{
MyMatrix temp(n, m);

for (int i = 0; i < n; i++)
{

    for (int j = 0; j < m; j++)
    {

        temp.matrix[i][j] = matrix[i][j] + mat1.matrix[i][j];

    }

}

return temp;

}

H file

class MyMatrix
{

private:

int **matrix;
int n, m;

public:

MyMatrix(int a, int b);
~MyMatrix();

void Set();

const MyMatrix  operator+ (const MyMatrix& mat1) const;
MyMatrix& operator= (const MyMatrix& mat1);

friend ostream& operator<<(ostream& out, const MyMatrix& matrix);
};
  • 2
    *Code works fine* -- No it doesn't. The destructor is exposing the bugs in your code. Put it back and fix the bugs. Second, where is your copy constructor for `Matrix`? Third, your assignment operator introduces a memory leak, and is way off as to the proper way to perform a copy of `Matrix`. – PaulMcKenzie Jan 18 '17 at 21:11
  • *SO i asked to create matrix class* -- You could have simply used `std::vector> matrix;` and not have any of the issues you're having now. – PaulMcKenzie Jan 18 '17 at 21:16
  • 1
    Worth reading: https://isocpp.org/wiki/faq/operator-overloading#matrix-subscript-op Note how it uses a 1D array and performs the math to do the 2D to 1D indexing. This is much faster and as you can see, much simpler than mucking about with arrays of arrays. – user4581301 Jan 18 '17 at 21:19
  • 3
    Also give [What is the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) a read. It will pre-emtively answer what's probably going to be the next question. – user4581301 Jan 18 '17 at 21:23
  • 1
    It is hard to answer a question like this without practically writing the code correctly. The current code needs much more than a couple of minor adjustments. – PaulMcKenzie Jan 18 '17 at 21:30

0 Answers0