"EXC_BAD_ACCESS" error while trying to use pointer classes :s

SO for my assignment, I had to make a matrix class, and using pointers, make the user enter a matrix, and then run the program to copy that matrix into another object and display both identical matrices. I have to use pointers for both matrices. Here's the 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
#include <iostream>
#include <fstream>
using namespace std;

class matrix
{
private:
    int rows;
    int cols;
    double** matrixPtr;
public:    
    matrix();
    matrix(int, int);
    matrix(int, int, double);
    matrix& operator =(matrix&);
    friend istream& operator >>(istream&,matrix&);
    friend ostream& operator <<(ostream&, matrix);
    double initial;

    //~matrix();
};


istream& operator >>(istream& getdata,matrix& array)
{
    if (array.matrixPtr!=0) 
    {
        for (int i = 0; i< array.rows; i++)
        {
            delete [] array.matrixPtr[i];
        }
        delete[] array.matrixPtr;
    }
    getdata >> array.rows >> array.cols;
    array.matrixPtr = new double* [array.rows];
    for (int i = 0; i<array.rows; i++)
    {
        array.matrixPtr[i] = new double[array.cols];
        for (int j= 0; j<array.cols; j++)
        {
            getdata >> array.matrixPtr[i][j];
        }
    }
    return getdata;
}

ostream& operator <<(ostream& showdata, matrix array)
{
    
    for (int i = 0; i<array.rows; i++)
    {
        for (int j=0; j<array.cols; j++)
        {
            showdata << array.matrixPtr[i][j] << " ";
        }
        showdata << endl;
    }
    return showdata;
}

matrix& matrix::operator =(matrix& array2)
{
    if (!array2.matrixPtr==NULL) 
    {
        for (int i = 0; i< array2.rows; i++)
        {
            delete [] array2.matrixPtr[i];
        }
        delete[] array2.matrixPtr;
    }
    
    
    array2.rows=rows;
    array2.cols=cols;
    array2.matrixPtr = new double* [array2.rows];
    
 for (int i = 0; i<rows; i++)
 {
    matrixPtr[i] = array2.matrixPtr[i];
 }
  for (int i = 0; i<rows; i++)
   {
       for (int j = 0; j<cols; j++)
       {
        array2.matrixPtr[i][j] = matrixPtr[i][j];
       }
   }
    return array2;
}

matrix::matrix()
{
    rows=0;
    cols=0;
    matrixPtr=NULL;
}

matrix::matrix(int row, int col)  
{
    rows= row;
    cols=col;
    matrixPtr = new double*[rows];
    for (int i = 0; i<rows; i++) 
    {
        matrixPtr[i] = new double[cols];
    }
    
    for (int i=0; i<row; i++)
    {
        for (int j=0; j<col; i++)
        {
            matrixPtr[i][j]=0.0;
        }
    }
    
}


matrix::matrix(int row, int col, double ini)
{
    ini = initial;
    for (int i=0; i<row; i++)
    {
        for (int j=0; j<col; i++)
        {
            matrixPtr[i][j]=initial;
        }
    }

}

int main()
{
   matrix thematrix, matrixreloaded;
   
    cout << "Enter the Number of Rows and Columns: ";
    cin >> thematrix;
    cout << thematrix;
    thematrix = matrixreloaded;
    cout << matrixreloaded;
    return 0;

}


The problem is in the assignment operator definition, in which the line " array2.matrixPtr[i][j] = matrixPtr[i][j];" gives an error saying "Thread 1: Program received signal: "EXC_BAD_ACCESS" "

Any help would be greatly appreciated! :)
Last edited on
Your assignment operator is backwards
Last edited on
Eh thanks man, But I guess that's not the only problem... My program is probably full of bugs... There are other things I'm trying to work around as well... :s like swapping stuff..

Topic archived. No new replies allowed.