C++ 重载运算符保持变量更新,而不是保持原始变量
我正在尝试对数组进行一些算术运算。比如数组[1,2,3]+[1,2,3]应该返回[2,4,6]。但是,当我尝试执行下一个算术[1,2,3]*[1,2,3]=时,它返回[4,16,36],而不是我想要的[1,4,9]。在我看来,原始数组被永久更新为第一次算术运算的结果。请帮助比概念支持更深一点,比如给我一些替代的编码建议,因为我正在学习C++,这是我遇到的最困难的章节。多谢各位C++ 重载运算符保持变量更新,而不是保持原始变量,c++,arrays,class,operator-overloading,C++,Arrays,Class,Operator Overloading,我正在尝试对数组进行一些算术运算。比如数组[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 <
#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;
}
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
二班
{
私人:
int-MaxCols;
双*外射线;
双常数;
公众:
TwoD(整数大小)
{
MaxCols=尺寸;
outerArray=新的双精度[MaxCols];
}
TwoD(整数大小,双康斯坦丁输入)
{
MaxCols=尺寸;
outerArray=新的双精度[MaxCols];
对于(int k=0;k>外层射线[k];
}
无效输出(整数大小)
{
对于(int l=0;l cout您需要停止修改原始值:
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;
}
consttwod运算符+(consttwod&右侧)const
//加上这个^^^^^
{
//创建适合保存结果的新对象
二维结果(MaxCols);
对于(int l=0;l 您是否尝试过调试?代码运行正常(表示没有崩溃/错误).它只返回我在问题中描述的不希望的结果。Thanks@UjaeKang调试还意味着单步执行代码并检查中间值,以发现程序的行为方式。Angew。也许我应该明确知道这一点。我的建议是停止现在正在做的事情,并学习如何调试。这是程序员(无论编程语言如何)必须具备的最高技能。好吧,它与“return result;”不起作用。所以我把“return*this”放回去,它仍然不起作用。你能至少向我解释一下为什么是“const”是添加在第一行的后端吗?@UjaeKangreturn*这;
仍然是错误的。更具体地说:什么不起作用?并且添加了const
,将方法本身标记为const
,这意味着它不会修改操作的左侧。如果您有TwoD result=lhs+rhs;
,则两者都不需要lhs
或rhs
要修改。@UjaeKang我还建议您将返回类型从const TwoD
更改为TwoD
。太好了!我明白了。现在我看到您上面建议的代码正在创建一个新对象,它正在返回新创建的对象。非常感谢您。@UjaeKang我喜欢您自己解决它,这是正确的精神。+1回答你的问题。我希望更多的人能接受你的学习方法,祝你成为程序员的旅途好运:)