Giving error at accessing the array, in a class Matrix

Please see the following code

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
#include<iostream>
#include<ctime>
using namespace std;
class Matrix
{
public:
	Matrix();
	Matrix(int r, int c);//----------overloaded
	Matrix(const Matrix &obj);
	~Matrix();
	//
	Matrix Add(Matrix m);
	Matrix Subtract(Matrix m);
	void input();
	void Print_Matrix();
	void Print(); 
private:
	int** p;
	int rows;
	int cols;
};
Matrix::Matrix()
{
	rows = 2;
	cols = 2;
	p = new int*[2];
	for (int i = 0; i < 2; i++)
		p[i] = new int[2];
}
Matrix::Matrix(int r, int c)
{
	rows = r;
	cols = c;
	p = new int*[rows];
	for (int i = 0; i < rows; i++)
		p[i] = new int[cols];
}
Matrix::Matrix(const Matrix &obj)
{
	this->rows = obj.rows;
	cols = obj.cols;  
	this->p = new int*[obj.rows]; 
	for (int i = 0; i < obj.rows; i++)
		p[i] = new int[obj.cols]; 

	for (int i = 0; i < obj.rows; i++)
	{
		for (int j = 0; j < obj.cols; j++)
			p[i][j] = obj.p[i][j]; 
	}

}
Matrix Matrix::Add(Matrix m) 
{
	Matrix P(rows, cols);
	if (rows == cols)
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				P.p[i][j] = this->p[i][j] + m.p[i][j];
			}
		}
		return P;
	}
	else
	{
		cout << "CAN'T BE ADDED\n";
		return P;
	}
}
Matrix Matrix::Subtract(Matrix m) 
{
	Matrix Q(rows, cols);
	if (rows == cols)
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < cols; j++)
			{
				Q.p[i][j] = this->p[i][j] - m.p[i][j];  
			}
		}
		return Q;
	} 
	else
	{
		cout << "CAN'T BE SUBTRACTED\n";
		return Q;
	}
}
void Matrix::input()
{
	cout << "INPUT :\n";
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			this->p[i][j] = rand() % 10 + 1;
		}
	}
}
void Matrix::Print_Matrix()
{
	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < cols; j++)
		{
			cout << "i: " << i << endl << "j: " << j << endl; 
			cout << this->p[i][j] << " ";
		}
		cout << endl;
	}
}
Matrix::~Matrix()
{
	for (int i = 0; i < rows; i++)
	{
		delete p[i];
	}
	delete[] p;
}

int main()
{


	int rows = 0, cols = 0;
	cout << "ENTER ROWS AND COLUMNS :";
	cin >> rows >> cols;
	if (rows == cols)
	{
		Matrix p1(rows, cols);
		Matrix p2(p1), p3(p1);
		cout << "ENTER FOR 1st:\n";
		p1.input();
		cout << endl;
		p1.Print_Matrix();

		cout << "ENTER FOR 2nd :\n ";
		p2.input();
		cout << endl;
		p2.Print_Matrix();

		cout << "ADDED :\n";
		p3 = p1.Add(p2);
		cout << endl;
		p3.Print_Matrix();
		cout << endl;
		cout << "SUBTRACTED :\n";
		p3 = p1.Subtract(p2);
		cout << endl;
		p3.Print_Matrix();

	}
	else
	{
		cout << "SIZES ARE DIFFERENT";
		return 0;
	}


	getchar();
	return 0; 
}

What it is doing is this that it is calling the destructor of Local Variable (P) made in "Add" function BEFORE copying the values of "P" in "p3" in int main...

I want to store the result of the add function in my int main object "p3" but because "P"'s destructor is already called... the result don't get stored and so, the compiler is giving error when it tries to print values of "p3".

anyone who probably can help solving this?
Last edited on
What's the problem? What does it do that you wish it didn't do, or what does it not do that you wish it did?
What it is doing is this that it is calling the destructor of Local Variable (P) made in "Add" function BEFORE copying the values of "P" in "p3" in int main...

I want to store the result of the add function in my int main object "p3" but because "P"'s destructor is already called... the result don't get stored and so, the compiler is giving error when it tries to print values of "p3".

anyone who probably can help solving this?
Topic archived. No new replies allowed.