I am using operator to add two matrices stored inside matrix objects. I instantiate and initialize two matrices. Then I call operator to add them. But before adding the two matrices: m1 and m2 , their destructors gets called. When I start adding the matrices since m2 matrix array has been deleted, it crashes. How can I force the destrucotr to get called on the matrices after I add the two matrices together.

// calling matrix initialization and addition
m1.initMatrix();
m1.print();
m2.initMatrix();
m2.print();
rslt = m1-m2;

//operator
matrix matrix::operator +(matrix m1)
{
	
        matrix rslt = getResultMatrix(rows, cols, 0); 

	for(int n=0; n<iterations; n++)
	{
		for(int i=0; i<rows; i++)
   		{
   			for(int j=0; j<cols; j++)
      			{
         			rslt.m[i][j] = this->m[i][j]+m1.m[i][j];
				
      			}

   		}
	}
	
	return rslt;

}
// destructor
matrix::~matrix()
{
		
	delete[] this->m;
}

on line 6 you are doing subtraction not addition. is that a typo or is this happening in you subtraction function?

I don't know what the rest of your program looks like, but consider and run this program:

#include <iostream>
#include <cstdio>
using namespace std;



class matrix
{
    public:
        int a;

    matrix ();
    matrix (int a);
    matrix (const matrix & m1);
    ~matrix ();
    matrix b (matrix c);
    matrix operator + (const matrix& m1) const;
    matrix & operator = (const matrix & m1);
};


//operator
matrix& matrix::operator =(const matrix& m1)
{
    printf ("Assignment : this=%p m1=%p a=%d\n", this, &m1, a);
    if (this == &m1)
    {
        cout << "Self assignment\n";
    }
    this->a = m1.a;
    return *this;
}


//operator
matrix matrix::operator +(const matrix& m1) const
{
    cout << "Here I am 2\n";
    matrix rslt (this->a + m1.a);
    cout << "Here I am 3\n";
	return rslt;
}


matrix::matrix (const matrix & m1)
{
    printf ("Copy Constructor : this=%p m1=%p a=%d\n", this, &m1, a);
}


matrix::matrix ()
{
    printf ("Null Constructor : this=%p a=%d\n", this, a);
}


matrix::matrix (int a)
{
    printf ("Constructor : this=%p a=%d\n", this, a);
    this->a = a;
}

// destructor
matrix::~matrix()
{
    printf ("Destructor : this=%p a=%d\n", this, a);
}


int main ()
{
    matrix m1 (7);
    matrix m2 (9);
    cout << "Here I am 1\n";
    matrix m3 = (m1 + m2);
    cout << "Here I am 4\n";
    cout << m3.a << endl;

    return 0;
}

Look at the output. Now look carefully at lines 17 and 36. Compare them to your program:

My line 36

matrix matrix::operator + (const matrix& m1) const

Your line:

matrix matrix::operator +(matrix m1)

Note the lack of an ampersand (and less importantly, the lack of const) in your code. Replace those lines with your lines of code. Note the call to the copy constructor when you get rid of the & and note the corresponding extra call to the destructor. If you have not written a copy constructor, the compiler will make one for you, but who knows whether it will be the one you desire. I would imagine you want to to make your operator similar to mine and avoid that extra constructor call. See if that solves the problem.

This article has been dead for over six months. Start a new discussion instead.