return class value

http://www.cplusplus.com/forum/beginner/7609/

I have virtually the same code as this old post, but the solution suggested does not work for me.

Debugging I see that the destructor is called when the operator+ tries to return the value and than no longer exists.

Post code?

If the solution worked for him and not for you... your code (and your problem) obviously must be different.
Last edited on
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
#include <iostream> 
#include <iomanip>
#include <stdlib.h> 

using namespace std; 
typedef int matrixData; 

class matrix 
{ 
friend ostream& operator<<(ostream&, const matrix&);
public:
    matrix(int row_input, int column_input);
    matrix();
    ~matrix(); 
    matrix operator+(const matrix&);
    matrix& operator=(const matrix&);
    matrix& operator+=(const matrix&);
    matrix operator-(const matrix&);
    matrix& operator-=(const matrix&);
    matrix operator*(const matrix&);
    matrix& operator*=(const matrix&);
    bool operator==(const matrix&);
    bool operator!=(const matrix&);

private:
           int row;            // the row size of the matrix    
        int column;         // the column size of the matrix
           matrixData **data;  // dynamically allocated array storage          
};


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
#include "matrix.h"

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

matrix::matrix(){
column=0;
row=0;
data=NULL;
}
matrix::matrix(int row_input, int column_input){
    data = new matrixData*[row_input];
    for(int i=0;i<row_input;i++){
        data[i]=new matrixData[column_input];
    }
    for(int i=0;i<row_input;i++){
        for(int j=0;j<column_input;j++){
            data[i][j]= (matrixData)(i-j);
        }
    }
    column=column_input;
    row=row_input;
}

matrix::~matrix(){
	for( int i=0;i<row;i++){
		delete [] data[i];
	}// Delete all cells in each row
	delete [] data; // Delete array of pointers
}

matrix matrix::operator+(const matrix& mat){
	matrix temp(row,column);
	for(int i=0;i<(row);i++){
		for(int j=0;j<(column);j++){
			temp.data[i][j]=data[i][j]+mat.data[i][j];
		}
	}
	return temp;
	
}

matrix matrix::operator*(const matrix& mat){
matrix temp(row, mat.column);

for(int j=0;j<temp.column;j++){
	for(int k=0;k<(row);k++){
		for(int m=0;m<(column);m++){
			temp.data[k][j]+=(data[k][m]*mat.data[m][j]);
		}					
	}	
}
return temp;
}

matrix& matrix::operator=(const matrix& mit){
	if(this!= &mit){
		for(int i=0;i<(row);i++){
			for(int j=0;j<(column);j++){
				data[i][j]= mit.data[i][j];
			}
		}
	}
	row=mit.row;
	column=mit.column;
	return *this;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "matrix.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main()
{
   matrix a(3, 4);
   matrix b(3, 4); 
   matrix c(3, 4); 
   matrix d(4, 3);
   matrix e(3, 3);

   c = a + b;


test code hits the operator= then has an access violation.
Last edited on
Hint:

If you need to write an assignment operator, then you also need to write a copy constructor.

The rule of 3 says that if your class has a non-trivial assignment operator, copy constructor,
or destructor, then it most likely needs all three.

You'll find that implementing a proper copy constructor will fix your program.
Topic archived. No new replies allowed.