gpt4 book ai didi

c++ - 重载 = 运算符时如何使用 THIS 指针

转载 作者:搜寻专家 更新时间:2023-10-31 00:41:39 28 4
gpt4 key购买 nike

所以我重载了 += 运算符,试图利用我已经定义的 + 运算符。即 Polynomial + Polynomial 返回一个新的 Polynomial。所以我的 += 函数基本上尝试调用 this + 函数,其中 LHS 为 *this,RHS 为 *this + B(其中 B 是通过 const 引用传递给函数的 Polynomial 对象)。我收到一个错误。我哪里错了?

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>

using namespace std;
void line(int lines);


class Polynomial
{
private:
int degree;
double* coeffs;
public:
//constructors
Polynomial() {degree=0;coeffs=new double[1];}
Polynomial(int deg) {degree=deg;coeffs=new double[deg+1];}
Polynomial(const Polynomial& A);

//mutators
void GetCoeffs(istream& in);
void EditCoeff(int deg);
void ResetCoeffs();
int Coeff(int deg);
void Randomize(int max);

//accessors
void Show(ostream& out);
int Degree() {return degree;}

//operators
Polynomial operator+(const Polynomial& B); //Polynomial + Polynomial
friend Polynomial operator +(double c, Polynomial& A); //c + Polynomial
Polynomial operator +(double c); //Polynomial + c
void operator +=(const Polynomial& B); //Polynomial+=Polynomial
void operator =(Polynomial& A);
Polynomial operator*(int k);
Polynomial operator*(Polynomial& A);

};



int main()
{
Polynomial A(5);
A.Randomize(4);
A.Show(cout);
line(2);
Polynomial B=A+8;
B.Show(cout);




return 0;
}

Polynomial Polynomial::operator*(int k)
{
Polynomial C(degree);
C=*this;
for (int i=degree; i>=0; i--)
C.coeffs[i]*=k;
return C;
}

Polynomial operator +(double c, Polynomial& A)
{
Polynomial C=A;
C.coeffs[0]+=c;
return C;
}
Polynomial Polynomial::operator +(double c)
{
Polynomial C=*this;
C.coeffs[0]+=c;
return C;
}

void Polynomial::Randomize(int max)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=rand()%(max+1) + 1;
if ((rand()%(101) + 1)%2 == 0)
coeffs[i]*=-1;
}
}

void Polynomial::operator =(Polynomial& A)
{
if (degree==A.degree)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
}

Polynomial Polynomial::operator+(const Polynomial& B)
{

if (degree>=B.degree)
{
Polynomial C(degree);
C=*this;

for (int i=B.degree; i>=0; i--)
{
C.coeffs[i]=coeffs[i]+B.coeffs[i];
}
return C;
}
else
{
Polynomial C=B;

for (int i=degree; i>=0; i--)
{
C.coeffs[i]=coeffs[i]+B.coeffs[i];
}
return C;

}

}

void Polynomial::operator+=(const Polynomial& B)
{
*this = (*this + B);
}

int Polynomial::Coeff(int deg)
{
return coeffs[deg];
}

void line(int lines)
{
for (int i=0; i<lines; i++)
cout << endl;
}

void Polynomial::GetCoeffs(istream& in)
{
for (int i=degree; i>=0; i--)
{
in >> coeffs[i];
}
in.ignore();
}

void Polynomial::Show(ostream& out)
{

if (coeffs[degree]>0)
cout << " ";
for (int i=degree; i>=0; i--)
{


if (coeffs[i]>=0)
{
if (i!=degree)
out << " + ";
out << coeffs[i];


}
else
{
if (coeffs[i]<0)
out << " - ";
out << 0-coeffs[i];
}
if (i>1)
out << "x^" << i;
else if (i==1)
out << "x";

}
}

Polynomial::Polynomial(const Polynomial& A)
{
coeffs=new double[A.degree+1];
degree=A.degree;
for (int i=A.degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];

}

}

Polynomial Polynomial::operator*(Polynomial& A)
{
int deg=A.degree+degree;
Polynomial P(deg);

for (int i=deg; i>=0; i--)
P.coeffs[i]=0;



for (int i=deg; i>=0; i--)
{
for (int j=A.degree; j>=0; j--)
{
P.coeffs[i+j]+=coeffs[i]*A.coeffs[j];
}
}

return P;


}

最佳答案

你的问题是 *this + B 是一个临时对象,临时对象不能绑定(bind)到非 const 引用。

当然,赋值的 RHS 没有理由不能是 const。尝试:

void operator=(/* HERE */ const Polynomial& A);

大多数其他运算符也没有在它们应该使用的地方使用 const。例如:

Polynomial operator+(const Polynomial& B) /* HERE */ const; //Polynomial + Polynomial
friend Polynomial operator +(double c, /* HERE */ const Polynomial& A); //c + Polynomial

只有赋值运算符应该是非const 成员,并且它们应该接受const 右操作数。创建新对象的普通二元运算符应该是 const wrt 左右操作数。

关于c++ - 重载 = 运算符时如何使用 THIS 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906695/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com