1

我在重载 + 运算符时遇到问题,我无法弄清楚原因是什么。+ 运算符返回一个多项式(称为 C),但它按值返回它,其中赋值运算符期望一个多项式对象作为通过引用传递的参数。为了使 C=A+B 工作,我是否需要第二个赋值运算符函数,它将按值传递的多项式作为参数?谢谢!

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

using namespace std;
void line(int lines);

class Polynomial
{
    private:
        int degree;
        int* coeffs;
    public:
        //constructors
        Polynomial() {degree=0;coeffs=new int[1];}
        Polynomial(int deg) {degree=deg;coeffs=new int[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
        friend Polynomial operator+(Polynomial& A, Polynomial& B);
        void operator =(Polynomial A);
};


int main()
{
    Polynomial A(5);
    Polynomial B(5);
    A.Randomize(5);
    B.Randomize(5);

    A.Show(cout);
    line(2);
    B.Show(cout);
    line(2);
    Polynomial C(5);
    C=A+B;
    C.Show(cout);

    return 0;
}

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+(Polynomial& A, Polynomial& B)
{
    Polynomial C;
    if (A.degree>=B.degree)
    {
        C=A;
        for (int i=B.degree; i>=0; i--)
        {
            C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
        }
        C.Show(cout);
        return C;
    }
    else
    {
        C=B;
        for (int i=A.degree; i>=0; i--)
        {
            C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
        }
        C.Show(cout);
        return C;

    }

}

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)
{
    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 int[A.degree+1];
    degree=A.degree;
    for (int i=A.degree; i>=0; i--)
    {
        coeffs[i]=A.coeffs[i];

    }

}
4

2 回答 2

0

这里的问题是您混淆了全局(外部类定义)operator+和成员类定义operator+

( static)Polynomial operator+(Polynomial& A, Polynomial& B);此运算符在全局范围内使用,因此在类之外。

在类中,您需要使用以下签名。

Polynomial& operator+(const Polynomial& other);

这是一个例子。

Polynomial p;
Polynomial q;
p = p + q;

如果在类中定义了运算符,则代码如下:

p = p.operator+(q); //only needs one parameter.

如果运算符是全局定义的,则代码为:

p = ::operator+(p, q); //needs both parameter

注意: 要将其用作非成员函数Polynomial operator+(Polynomial& A, Polynomial& B);,从您的定义中删除Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B){/**/}应该移到主函数之上,它现在变为:

static Polynomial operator+(Polynomial& A, Polynomial& B){/**/}

于 2012-10-13T20:42:17.530 回答
0

通常,您会operator+通过以下两种方式之一超载:

Polynomial operator+(const Polynomial &other) const;

或作为非成员函数,

Polynomial operator+(Polynomial a, const Polynomial &b);

您通常不需要friend后者的限定符,因为实现可能是根据已在类中定义的另一个重载运算符:

Polynomial operator+=(const Polynomial &other);

那么您的非成员实现将是:

Polynomial operator+(Polynomial a, const Polynomial &b)
{
     a+=b;
     return a;
}

如果您坚持使用代码中定义的函数,那么如果friend需要访问私有成员,则需要将其设为 a :

Polynomial operator+(Polynomial &a, Polynomial &b)
{
     Polynomial p;
     // add them as needed
     return p;
}
于 2012-10-13T22:30:52.117 回答