我一直在尝试添加两个二维数组。在我为“=”添加重载函数后,C++ 开始崩溃一次。编译器只是崩溃。但是没有添加
cout << "combining the two is" << endl;
example3 = example1 + example2;
example3.outPut();
并禁用重载运算符成员函数,一切正常。
能否请你帮忙?
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];
}
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;
}