拷贝构造函数三巨头C ++的问题 [英] Copy Constructor Big 3 C++ issue
本文介绍了拷贝构造函数三巨头C ++的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我与实施3大理念挣扎(赋值操作符重载,拷贝构造函数,析构函数)。下面我code会使程序崩溃。它编译,所以我甚至不能看到任何错误提示。请帮忙。
在此输入code#包括LT&;&iostream的GT;
使用命名空间std;
#包括LT&;串GT;
#包括LT&;&的fstream GT;
#包括LT&;&CMATH GT;
#包括LT&; cstdlib>
#包括LT&;&的ctime GT;
#包括LT&;矢量>
#包括LT&;&WINDOWS.H GT;
#包括LT&; CString的GT;
#包括LT&;&cctype GT;
#包括LT&;&了iomanip GT;
#包括LT&;&算法GT;
#包括LT&;&sstream GT;
类数组twoD
{
私人的:
INT MAXROWS;
INT MaxCols;
双** outerArray;上市:
数组twoD(INT maxRows进行,诠释maxCols)
{
MAXROWS = maxRows进行;
MaxCols = maxCols;
outerArray =新的双* [MAXROWS]
的for(int i = 0; I< MAXROWS,我++)
outerArray [I] =新的双[MaxCols]
}数组twoD(常量数组twoD&安培; rightside)
{
对于(int类型l = 0; L< MAXROWS,L ++)
对于(INT M = 0; M< MaxCols; M +)
outerArray [L] [M] = rightside.outerArray [L] [M];
}输入无效()
{
对于(INT K = 0; K< MAXROWS; k ++)
对于(INT J = 0; J< MaxCols; J ++)
CIN>> outerArray [K] [J]。
}无效输出()
{
对于(int类型l = 0; L< MAXROWS,L ++)
{
对于(INT M = 0; M< MaxCols; M +)
COUT<< outerArray [L] [M]下;&下; ;
COUT<< ENDL;
}
}常量数组twoD&安培;运算符=(常量数组twoD&安培; rightSide)
{
对于(int类型l = 0; L< MAXROWS,L ++)
{
对于(INT M = 0; M< MaxCols; M +)
outerArray [L] [M] = rightSide.outerArray [L] [M];
COUT<< ENDL;
} 返回*这一点;
}常量数组twoD运营商+(常量数组twoD&安培; rightSide)
{
对于(int类型l = 0; L< MAXROWS,L ++)
{
对于(INT M = 0; M< MaxCols; M +)
outerArray [L] [M] = outerArray [L] [M] + rightSide.outerArray [L] [M];
COUT<< ENDL;
} 返回*这一点;
}〜数组twoD()
{
的for(int i = 0; I< MAXROWS,我++)
删除[] outerArray [I]
删除[] outerArray;
}};诠释的main()
{
数组twoD例1(3,3),例2(3,3),例3(3,3);
COUT<< 输入例1<< ENDL;
example1.input();
example1.outPut();COUT<< 输入例2<< ENDL;
example2.input();
example2.outPut();COUT<< 两者结合是<< ENDL;
例3例1 = +示例2;
example3.outPut();
返回0;
}
修改拷贝构造函数
数组twoD(常量数组twoD&安培; rightside):MAXROWS(rightside.MaxRows),MaxCols(rightside.MaxCols)
{
outerArray =新的双* [MAXROWS]
的for(int i = 0; I< MAXROWS,我++)
outerArray [I] =新的双[MaxCols] 对于(int类型l = 0; L< MAXROWS,L ++)
对于(INT M = 0; M< MaxCols; M +)
outerArray [L] [M] = rightside.outerArray [L] [M];
}
解决方案
- 您的拷贝构造函数不能为矩阵分配内存。
- 您的拷贝赋值运算符和
操作符+()
无法解释的可能性,*本
和rightSide
有不同的尺寸。
I am struggling with implementing big 3 idea (assignment operator overload, copy constructor, destructor). my code below will crash the program. It compiles so I cannot even see any error hint. Please help.
enter code here
#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <windows.h>
#include <cstring>
#include <cctype>
#include <iomanip>
#include <algorithm>
#include<sstream>
class TwoD
{
private:
int MaxRows;
int MaxCols;
double** outerArray;
public:
TwoD(int maxRows, int maxCols)
{
MaxRows = maxRows;
MaxCols = maxCols;
outerArray = new double *[MaxRows];
for (int i = 0; i < MaxRows; i++)
outerArray[i] = new double[MaxCols];
}
TwoD(const TwoD& rightside)
{
for (int l = 0; l < MaxRows; l++)
for (int m = 0; m < MaxCols; m++)
outerArray[l][m] = rightside.outerArray[l][m];
}
void input()
{
for (int k = 0; k < MaxRows; k++)
for (int j = 0; j < MaxCols; j++)
cin >> outerArray[k][j];
}
void outPut()
{
for (int l = 0; l < MaxRows; l++)
{
for (int m = 0; m < MaxCols; m++)
cout << outerArray[l][m] << " ";
cout << endl;
}
}
const TwoD& operator =(const TwoD& rightSide)
{
for (int l = 0; l < MaxRows; l++)
{
for (int m = 0; m < MaxCols; m++)
outerArray[l][m] = rightSide.outerArray[l][m];
cout << endl;
}
return *this;
}
const TwoD operator + (const TwoD& rightSide)
{
for (int l = 0; l < MaxRows; l++)
{
for (int m = 0; m < MaxCols; m++)
outerArray[l][m] = outerArray[l][m] + rightSide.outerArray[l][m];
cout << endl;
}
return *this;
}
~TwoD()
{
for (int i = 0; i < MaxRows; i++)
delete[] outerArray[i];
delete[] outerArray;
}
};
int main()
{
TwoD example1(3, 3), example2(3,3), example3(3,3);
cout << "input example1" << endl;
example1.input();
example1.outPut();
cout << "input example2" << endl;
example2.input();
example2.outPut();
cout << "combining the two is" << endl;
example3 = example1 + example2;
example3.outPut();
return 0;
}
changed copy constructor
TwoD(const TwoD& rightside): MaxRows(rightside.MaxRows), MaxCols(rightside.MaxCols)
{
outerArray = new double *[MaxRows];
for (int i = 0; i < MaxRows; i++)
outerArray[i] = new double[MaxCols];
for (int l = 0; l < MaxRows; l++)
for (int m = 0; m < MaxCols; m++)
outerArray[l][m] = rightside.outerArray[l][m];
}
解决方案
- Your copy constructor fails to allocate memory for the matrix.
- Your copy assignment operator and
operator+()
fail to account for the possibility that*this
andrightSide
have different dimensions.
这篇关于拷贝构造函数三巨头C ++的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文