c++ - 赋值运算符重载后的分段错误(核心转储)错误
问题描述
我的编程任务希望我创建一个带有复制构造函数的类并重载赋值运算符 (=)。我的代码运行平稳,直到我使用重载赋值运算符,然后它返回错误,分段错误(核心转储)。
当我将其更改为使用复制构造函数时,它不会返回任何错误,因此我认为问题出在运算符重载部分。知道是什么原因造成的吗?如果是算子重载部分,有什么问题?任何帮助将不胜感激,谢谢。
#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)
解决方案
推荐阅读
- r - 用r中的奇怪字符解析数字
- javascript - innerHTML 没有出现
- python - How to remove space in a value
- linux - 如何找到可执行文件所在的目录,显示出来,不重复?Linux
- c++ - 为什么记忆的解决方案比正常的递归解决方案慢?
- javascript - 通过 SSH 使用 exec 的异步等待功能
- python - 如何使用 BLEU 将假设与参考相匹配?
- react-native - React Native Flatlist 仅更新最后一项
- reporting-services - SSRS manager VS Report builder中的共享数据源
- linq - 如何使两个联合的 LINQ 语句异步?