首页 > 解决方案 > 赋值运算符重载后的分段错误(核心转储)错误

问题描述

我的编程任务希望我创建一个带有复制构造函数的类并重载赋值运算符 (=)。我的代码运行平稳,直到我使用重载赋值运算符,然后它返回错误,分段错误(核心转储)。

当我将其更改为使用复制构造函数时,它不会返回任何错误,因此我认为问题出在运算符重载部分。知道是什么原因造成的吗?如果是算子重载部分,有什么问题?任何帮助将不胜感激,谢谢。

#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;
}

此处的运算符重载

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];
        }
    }
}

主要在这里

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;
}

预期结果:

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

实际结果:

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)

标签: c++arrays

解决方案


推荐阅读