Dynamic Array and Operation Overload

I am having a hard time figuring out why my Overloaded operation does not return what I am looking for. If I display my matrix right after the calculations, it works just fine. I cannot figure out why I can't return the class and display the matrix. Is my destructor deleting the pointer in my class when it gets returned? (I do not think that would cause a shallow copy?) I just cannot see what I am missing. Thank you in advance guys.


Here is my implementation file

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

int matrixClass::getColumns()
{
    return columns;
}
int matrixClass::getRows()
{
    return rows;
}
void matrixClass::setRows(int r)
{
    rows = r;
}

void matrixClass::setColumns(int c)
{
    columns = c;
}
void matrixClass::setMatrix()
{
    matrix = new double* [rows];

    for(int x = 0; x < rows; x++)
        matrix[x] = new double[columns];

    for(int x = 0; x < rows; x++)
        for(int j = 0; j < columns; j++)
        {
            cout << "Element [" << x << "]" << "[" << j << "]" << endl;
            cin >> matrix[x][j];
        }

}
void matrixClass::displayMatrix() const
{
    for(int x = 0; x < rows; x++)
    {
        for(int j = 0; j < columns; j++)
        {
            cout << matrix[x][j] << " ";
        }
        cout << endl;
    }
}

matrixClass matrixClass::operator*
                    (matrixClass& matrixTWO)
{
    matrixClass matrixTHREE(getColumns(), matrixTWO.getRows());

    for (int x = 0; x < rows; x++)
    {
      for (int j = 0; j < columns; j++)
      {
		 matrixTHREE.matrix[x][j] = 0;
         for (int y = 0; y < rows; y++)
         {
            matrixTHREE.matrix[x][j] += matrix[x][y] * matrixTWO.matrix[y][j];
         }
      }

    }

    return(matrixTHREE);
}

matrixClass::~matrixClass()
{
    for(int x = 0; x < rows; x++)
        delete [] matrix[x];

        delete [] matrix;

}
matrixClass::matrixClass(int r, int c)
{
    if(r >= 0) rows = r;
    else rows = 0;

    if(c >= 0) columns = c;
    else columns = 0;

    matrix = new double*[rows];

    for(int x = 0; x < rows; x++)
        matrix[x] = new double[columns];


}


Header
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
#ifndef MATRIX_H
#define MATRIX_H


#include <iostream>

using namespace std;

class matrixClass
{
    public:

        int getColumns();
        int getRows();
        void setRows(int r);
        //Sets rows
        void setColumns(int c);
        //Set columns
        void setMatrix();
        //Sets the matrix
        void displayMatrix() const;
        //Displays the matrix

        matrixClass operator *(matrixClass&);
        //Multiplication overload
        matrixClass operator -(const matrixClass&);
        //Subtraction overload
        matrixClass operator +(const matrixClass&);
        //Addition overload
        matrixClass(int r = 0, int c = 0);
        //Default constructor
        ~matrixClass();
        //Destructor

    private:

        int rows;
        int columns;
        double** matrix;
};

#endif // MATRIX_H 


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

matrixClass m1;
matrixClass m2;
matrixClass m3;

int main()
{
 int rows, columns;


 cout << "Enter numbers of rows\n";
 cin >> rows;
 m1.setRows(rows);

 cout << "Enter number of columns\n";
 cin >> columns;
 m1.setColumns(columns);

 m2.setRows(m1.getRows());
 m2.setColumns(m1.getColumns());

 m3.setRows(m1.getRows());
 m3.setColumns(m1.getColumns());

 cout << "Enter the elements for the first matrix\n";
 m1.setMatrix();

 cout << "Enter the elements of the second matrix\n";
 m2.setMatrix();

 m3 = m1 * m2;

 m1.displayMatrix();
 m2.displayMatrix();
 m3.displayMatrix();


}

You don't provide a copy constructor or assignment operator. So when you perform these operations, you get the default, which is copies of all your members.

Some of your members are pointers, so you get two instances pointing to the same stuff. When one goes, it deletes them, leaving the other pointing to delete memory, dangling references. If the heap reuses those addresses, it'll corrupt the state of that remaining instance.
Topic archived. No new replies allowed.