简体   繁体   English

复制构造函数Big 3 C ++问题

[英]Copy Constructor Big 3 C++ issue

I am struggling with implementing big 3 idea (assignment operator overload, copy constructor, destructor). 我正在努力实现大3的想法(赋值运算符重载,复制构造函数,析构函数)。 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];    
}
  1. Your copy constructor fails to allocate memory for the matrix. 您的复制构造函数无法为矩阵分配内存。
  2. Your copy assignment operator and operator+() fail to account for the possibility that *this and rightSide have different dimensions. 您的副本分配运算符和operator+()无法说明*thisrightSide具有不同尺寸的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM