所以我以一种试图利用我已定义的+运算符的方式重载了+ =运算符。即Polynomial + Polynomial返回一个新的多项式。所以我的+ =函数基本上试图调用这个+函数,LHS为* this,RHS为* this + B(其中B是由const引用传递给函数的多项式对象)。我收到了一个错误。我哪里错了?
#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;
}
答案 0 :(得分:3)
您的问题是*this + B
是临时的,临时对象不能绑定到非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
左右操作数。
答案 1 :(得分:1)
通常以其他方式完成。人们使用二元+运算符(通常是非成员)来利用已定义的+ =运算符(成员)。
答案 2 :(得分:1)
我在哪里错了?
std::vector
而不是原始指针。operator+=
和免费operator+
。试试这个:
// UNTESTED
class Polynomial
{
private:
std::vector<double> coeffs;
public:
//constructors
Polynomial() : coeffs(1) {}
Polynomial(int deg) : coeffs(deg+1) {}
// Don't need copy constructor
// Don't need destructor
// Don't need assignment operator
...
int Degree() {return coeffs.size()-1;}
//operators
Polynomial& operator+=(const Polynomial& B) {
if(Degree() < B.Degree())
coeffs.resize(B.Degree()+1);
for(int i = 0; i <= B.Degree(); ++i)
coeffs[i] += B.coeffs[i];
return *this;
}
};
Polynomial operator+(Polynomial A, const Polynomial& B) {
return A += B;
}