2012-10-13 65 views
1

我無法重載+運算符,我找不出原因是什麼。 +運算符返回一個Polynomial(稱爲C),但它通過值返回它,因爲賦值運算符期望Polynomial對象作爲通過引用傳遞的參數。爲了使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]; 

    } 

} 

回答

0

這裏這個問題你已經混了全球(外類定義)operator+operator+成員類定義。

staticPolynomial 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){/**/}

+0

對不起,但我不明白你的答案。當我使用typeid(A + B).name()時,它表示A + B正在返回Polynomial類型的變量。這很好,因爲我的賦值操作符用於將一個多項式分配給另一個多項式。如果我做C = A,那麼它可以工作,那麼爲什麼C = A + B不工作? – BrownBeard93423

+0

只需從運算符+定義中刪除',Polynomial&B',您的代碼就可以工作。 – andre

+0

但是operator +不是一個成員函數,所以它不需要將Polynomial對象都傳遞給它? – BrownBeard93423

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如果你需要訪問私有成員:

Polynomial operator+(Polynomial &a, Polynomial &b) 
{ 
    Polynomial p; 
    // add them as needed 
    return p; 
}