1

我正在尝试对数组进行一些算术运算。例如数组 [1,2,3] + [1,2,3] 应该返回 [2,4,6]。然而,当我尝试做下一个算术 [1,2,3] * [1,2,3] = 它返回 [4,16,36] 而不是我想要的 [1,4,9] 。在我看来,原始数组已永久更新为第一个算术运算的结果。请提供比概念支持更深入的帮助,例如为我提供一些替代编码建议,因为我正在学习 C++,这是我遇到的最艰难的一章。非常感谢你。

#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 MaxCols;
    double* outerArray;
    double constant;

public:
    TwoD(int size)
    {
        MaxCols = size;
        outerArray = new double[MaxCols];
    }
    TwoD(int size, double constantInput)
    {
        MaxCols = size;
        outerArray = new double[MaxCols];
        for (int k = 0; k < size; k++)
            outerArray[k] = constantInput;
    }
    TwoD(const TwoD& other): MaxCols(other.MaxCols)
    {
        outerArray = new double[MaxCols];

        for (int i = 0; i < MaxCols; i++)
            outerArray[i] = other.outerArray[i];
    }
    const TwoD& operator =(const TwoD& rightSide)
    {
        if(MaxCols != rightSide.MaxCols)
        {
            delete [] outerArray;
            outerArray = new double [rightSide.MaxCols];
        }
        MaxCols = rightSide.MaxCols;

        for (int i = 0; i < MaxCols; i++)
            outerArray[i] = rightSide.outerArray[i];

        return *this;
    }
    void input(int size)
    {
        for (int k = 0; k < size; k++)
                cin >> outerArray[k];
    }
    void outPut(int size)
    {
        for (int l = 0; l < size; l++)
        {
            cout << outerArray[l]<< ", ";
        }
        cout << endl;
    }
    const TwoD operator + (const TwoD& rightSide)
    {

        for (int l = 0; l < MaxCols; l++)
        {
            outerArray[l] = outerArray[l] + rightSide.outerArray[l];
            cout << endl;
        }

        return *this;
    }
    const TwoD operator * (const TwoD& rightSide)
    {
        for (int l = 0; l < MaxCols; l++)
        {
            outerArray[l] = outerArray[l] * rightSide.outerArray[l];
            cout << endl;
        }

        return *this;
    }
    ~TwoD()
    {
        delete[] outerArray;
    }
};

int main()
{
    int size;
    double constantInput;
    cout << "Please enter a size of an array" << endl;
    cin >> size;

    TwoD example1(size);
    cout << "input example1" << endl;
    example1.input(size);
    example1.outPut(size);

    cout << "Please enter a constant to work with: "<< endl;
    cin >> constantInput;
    TwoD constant1(size, constantInput);
    constant1.outPut(size);

    cout << "polynomial + polynomial" << endl;
    TwoD result1 = example1 + example1;
    result1.outPut(size);

    cout << "polynomial * polynomial" << endl;
    TwoD result2 = example1 * example1;
    result2.outPut(size);


    return 0;
}
4

1 回答 1

2

您需要停止修改原始值:

const TwoD operator + (const TwoD& rightSide) const
//                                   add this ^^^^^
{
    // create a new object suitable to hold the result
    TwoD result(MaxCols);
    for (int l = 0; l < MaxCols; l++)
    {
        // set the values in the result
        result.outerArray[l] = outerArray[l] + rightSide.outerArray[l];
        cout << endl;
    }
    // and finally return it
    return result;
}
于 2013-09-14T09:43:44.417 回答