please someone help me to define this destructor function

I made a program to multiply two matrix but I don't know how to define that destructor function. thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 //
//  main.cpp
//  tpc 7-5 matrix
//
//  Created by Wagner Pereira on 01/05/14.
//  Copyright (c) 2014 Wagner Pereira. All rights reserved.
//

#include <iostream>
using namespace std;

//Matrix 类的定义
class Matrix
{
    int ** matrix;
    int line, column;
    
    public:
        Matrix(int l = 0, int c = 0);
        void Input();
        void Print();
        ~Matrix();
        Matrix operator+ (const Matrix &obj1);      //operator+ 的声明
        Matrix operator- (const Matrix &obj1);      //operator- 的声明
        Matrix operator* (const Matrix &obj1);      //operator* 的声明
};

// Constructor function  ------------------------------
Matrix::Matrix(int l, int c)
{
    this->line = l;
    this->column = c;
    
    //根据矩阵的大小分配空间
    matrix = new int*[this->line];
    int i;
    for(i=0; i<this->line; i++)
    {
        matrix[i] = new int[this->column];
    }
    //如果分配失败则抛出异常
    if (!this->matrix)
        throw 0;
}

//Input function --------------------------------------
void Matrix:: Input()
{
    int l, c;       //定义 l 表示行 和 c 表示列
    static int count = 0;
    
    cout <<"Fill the "<<++count<<"ª matrix: "<<endl;
    
    //使用循环来输入矩阵的数据
    for (l = 0; l < this->line; l++)
    {
        cout <<l+1<<"ª line: ";
        for (c = 0; c < this->column; c++)
            cin>>this->matrix[l][c];
    }
    cout<<endl;
}

//Print function --------------------------------------
void Matrix:: Print()
{
    int l, c;
    cout<<endl;
    for (l = 0; l < this->line; l++)
    {
        for (c = 0; c < this->column; c++)
            cout << this->matrix[l][c]<<"\t";
        cout<<endl;
    }
    cout<<endl;
}

//Destructor function  --------------------------------
Matrix::~Matrix()
{/*
    int i;
    for (i = 0; i <this->line; i++)
    {
        delete[] matrix[i];
    }*/
    delete [] matrix;
}

//Operator +  -----------------------------------------
Matrix Matrix:: operator +(const Matrix &obj1)
{
    //判断两个矩阵是否同型,如果不同型则两矩阵不能进行加法运算所以抛出异常
    if (obj1.line == this->line && obj1.column == this->column)
    {
        int l, c;
        Matrix temp(this->line,this->column);
        
        for(l = 0; l < this->line; l++)
        {
            for(c =  0; c < this->column; c++)
                temp.matrix[l][c] = this->matrix[l][c] + obj1.matrix[l][c];
                
        }
        return temp;
    }
    else
        throw 1;
}

//Operator -  -----------------------------------------
Matrix Matrix:: operator -(const Matrix &obj1)
{
    //判断两个矩阵是否同型,如果不同型则两矩阵不能进行减法运算所以抛出异常
    if (obj1.line == this->line && obj1.column == this->column)
    {
        int l, c;
        Matrix temp(this->line,this->column);
        
        for(l = 0; l < this->line; l++)
        {
            for(c =  0; c < this->column; c++)
                temp.matrix[l][c] = this->matrix[l][c] - obj1.matrix[l][c];
        }
        return temp;
    }
    else
        throw 2;
}

//Operator * ------------------------------------------
Matrix Matrix:: operator* (const Matrix &obj1)
{
    //判断第一个矩阵的列是否等于第二个矩阵的行,如果相等则进行乘法,如不相等抛出异常
    if(this->column == obj1.line)
    {
        
        Matrix temp(this->line,obj1.column);
        int l,c,i;
        
        for (l = 0; l < this->line; l++)
            for (c = 0; c < this->line; c++)
                for (i = 0; i < this->column; i++)
                    temp.matrix[l][c] += this->matrix[l][i] * obj1.matrix[i][c];
        return temp;
    }
    else
        throw 3;
}


int main()
{

    try
    {
        Matrix m1(2,2);
        Matrix m2(2,2);
        Matrix m3(2,2);
    
    
        m1.Input();
        m2.Input();
        m3 = m1*m2;
        m3.Print();
        m3 = m1 + m2;
        m3.Print();
        
        m3 = m3 - m1;
        
    }
    catch(int x)
    {
        switch(x)
        {
            case 0:
                cout <<"Allocation failure"<<endl;
                cout<<"Program finished"<<endl;
                break;
            case 1:
                cout <<"The two matrices cannot be added!"<<endl;
                cout<<"Program finished"<<endl;
                break;
            case 2:
                cout <<"The two matrices cannot be added!"<<endl;
                cout<<"Program finished"<<endl;
                break;
            case 3:
                cout <<"The two matrices cannot be multiplied!"<<endl;
                cout<<"Program finished"<<endl;
                break;
        }
    }
    
    return 0;
}
You don't need to use this->

1
2
3
4
5
6
7
8
9
Matrix::~Matrix ( )
{
	size_t i, size = line * column;

	for ( i = 0; i < size; ++i)
		delete [] matrix[i];

	delete [] matrix;
}
Easiest way to write a destructor for this would be to just go in the reverse order of your constructor.

In your constructor, you do this:
35
36
37
38
39
40
matrix = new int*[this->line];
int i;
for(i=0; i<this->line; i++)
{
    matrix[i] = new int[this->column];
}
So your destructor should just be something like this:
1
2
3
4
5
for (int i = 0; i < this->line; i++)
{
    delete[] matrix[i];
}
delete[] matrix;
...basically what you have commented out.
Why is that commented out, anyways?
Did it not work for you?
Thank you guys but it still reporting error. I tried using Xcode and devc++ but it still the same problem.
What is the error?
matrix(331,0x7fff703ad310) malloc: *** error for object 0x100200010: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
(lldb)
Topic archived. No new replies allowed.