My programming assignment wanted me to make a class with a copy constructor and to overload the assignment operator (=). My code runs smoothly until I use the overloaded assignment operator, then it returns an error, segmentation fault (core dumped).
When I change it to use the copy constructor it doesn't return any errors, so I assume the problem is within the operator overloading part. Any idea what caused it? If it is the operator overloading part, what's the problem? Any help would be appreciated, thanks.
#define DefRow 2
#define DefCol 2
typedef double* DouArray;
class TwoD{
public:
TwoD();
TwoD(const TwoD& one); //Copy Constructor
~TwoD();
void setElement(int row, int column);
double getElement(int row, int column);
int getRows();
int getCols();
TwoD operator =(const TwoD& a);
private:
double** Matrix;
int MaxRows, MaxCols;
};
TwoD::TwoD(void){
MaxRows = DefRow;
MaxCols = DefCol;
Matrix = new DouArray[MaxRows];
for(int i = 0; i < MaxRows; i++){
Matrix[i] = new double[MaxCols];
}
}
TwoD::TwoD(const TwoD& one){
MaxRows = one.MaxRows;
MaxCols = one.MaxCols;
Matrix = new DouArray[MaxRows];
for(int i = 0; i < MaxRows; i++){
Matrix[i] = new double[MaxCols];
}
for(int i = 0; i < MaxRows; i++){
for(int j = 0; j < MaxCols; j++){
Matrix[i][j] = one.Matrix[i][j];
}
}
}
TwoD::~TwoD(void){
for(int i = 0; i < MaxRows; i++){
delete [] Matrix[i];
}
delete [] Matrix;
Matrix = NULL;
MaxRows = 0;
MaxCols = 0;
}
void TwoD::setElement(int row, int column){
cin >> Matrix[row][column];
}
double TwoD::getElement(int row, int column){
return Matrix[row][column];
}
int TwoD::getRows(){
return MaxRows;
}
int TwoD::getCols(){
return MaxCols;
}
operator overload here
TwoD TwoD::operator =(const TwoD& a){
MaxRows = a.MaxRows;
MaxCols = a.MaxCols;
for(int i = 0; i < MaxRows; i++){
for(int j = 0; j < MaxCols; j++){
Matrix[i][j] = a.Matrix[i][j];
}
}
}
main here
int main(){
int inrow, incol;
cout << "Enter the row and column dimensions of the array." << endl;
cin >> inrow >> incol;
TwoD matrix2(inrow, incol);
cout << "Enter " << inrow << " rows of " << incol << " doubles each." << endl;
for(int i = 0; i < inrow; i++){
for(int j = 0; j < incol; j++){
matrix2.setElement(i, j);
}
}
TwoD temp2 = matrix2;
cout << "Echoing the 2 dim. array, matrix2" << endl;
for(int i = 0; i < temp2.getRows(); i++){
for(int j = 0; j < temp2.getCols(); j++){
cout << temp2.getElement(i, j) << " ";
}
cout << endl;
}
cout << "Assigning matrix2 to matrix3" << endl;
TwoD matrix3;
matrix3 = matrix2; //I got the error here
cout << "Displaying the 2 dim. array, matrix3 resulting from assignment" << endl;
cout << "Rows " << matrix3.getRows() << " Cols " << matrix3.getCols() << endl;
for(int i = 0; i < matrix3.getRows(); i++){
for(int j = 0; j < matrix3.getCols(); j++){
cout << matrix3.getElement(i, j) << " ";
}
cout << endl;
}
return 0;
}
Expected result:
Enter the row and column dimensions of the array
3 4
Enter 3 rows of 4 doubles each.
1.0 1.0 1.0 1.0
2.0 2.0 2.0 2.0
3.0 3.0 3.0 3.0
Echoing the 2 dim. array, matrix2
1 1 1 1
2 2 2 2
3 3 3 3
Assigning matrix2 to matrix3
Displaying the 2 dim. array, matrix3 resulting from assignment
Rows 3 Cols 4
1 1 1 1
2 2 2 2
3 3 3 3
Actual result:
Enter the row and column dimensions of the array.
3 4
Enter 3 rows of 4 doubles each.
1.0 1.0 1.0 1.0
2.0 2.0 2.0 2.0
3.0 3.0 3.0 3.0
Echoing the 2 dim. array, matrix2
1 1 1 1
2 2 2 2
3 3 3 3
Assigning matrix2 to matrix3
Segmentation fault (core dumped)