2017-02-13 360 views
0

我在代碼中重載了postincrement(a ++)運算符,我試圖重載代碼中的預增量運算符(++ a)來執行函數將複數「a」進行平方,但是,此時只有後增加算子正在工作。我應該明確地說明,前後增量操作符都調用相同的函數 - 它們都應該使用方法/函數。所以他們應該給我同樣的答案,儘管在後/前職位。當我們調用preincrement運算符時,在它下面有一條紅線,表示:「不止一個運算符++匹配這些運算符,但我已經明確地提出了」++()「和」++(int)「考慮到這兩種++和A ++。重載預增量運算符錯誤「多個運算符++與這些操作數匹配」

我缺少的東西嗎?這裏是我的代碼。

#include<iostream> 
#include<iomanip> 
using namespace std; 

class ComplexNum 
{ 
public: 
    ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main 
    ComplexNum& getComplexNum(); //get real and imaginary numbers from keyboard 
    ComplexNum& sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together 
    ComplexNum& diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers 
    ComplexNum& prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers 
    ComplexNum& square(ComplexNum a); //method to find square using pre/post increment operators 

    //overloaded operators 
    ComplexNum& operator = (const ComplexNum& that) = default; 
    ComplexNum& operator += (const ComplexNum& that) { return sum(*this, that); } 
    ComplexNum& operator -= (const ComplexNum& that) { return diff(*this, that); } 
    ComplexNum& operator *= (const ComplexNum& that) { return prod(*this, that); } 
    ComplexNum& operator ++() { return square(*this); } //called for ++num 
    ComplexNum operator ++(int) { return square(*this); } //called for num++ 

    ostream& print(ostream& stm = cout) const; 


private: 
    float real; //float data member for real number (to be entered in by user) 
    float imaginary; //float data member for imaginary number (to be entered in by user) 

    //non-member overloaded operators 
    //a is passed by value 
    friend ComplexNum operator+ (ComplexNum a, const ComplexNum& b) { return a += b; } 
    friend ComplexNum operator- (ComplexNum a, const ComplexNum& b) { return a -= b; } 
    friend ComplexNum operator* (ComplexNum a, const ComplexNum& b) { return a *= b; } 
    friend ComplexNum operator++(ComplexNum a) { return a++; } //friend for num ++ 
    friend ComplexNum operator++(ComplexNum a) { return ++a; } //friend for ++num 

    friend ostream& operator<< (ostream& stm, const ComplexNum& c) { return c.print(stm); } 
}; 

ComplexNum::ComplexNum(float a, float b) 
{ 
    real = a; 
    imaginary = b; 
} 

ComplexNum& ComplexNum::getComplexNum() 
{ 
    ComplexNum keyboard; 
    cout << "Enter real part of complex number: "; 
    cin >> real; 

    cout << "Enter imaginary part of complex number: "; 
    cin >> imaginary; 

    return keyboard; 
} 

ComplexNum& ComplexNum::square(ComplexNum a) 
{ 
    this->real = (a.real * a.real) - (a.imaginary * a.imaginary); 
    this->imaginary = (2 * (a.real * a.imaginary)); 
    return *this; 
} 

ComplexNum& ComplexNum::sum(ComplexNum a, ComplexNum b) 
{ 
    this->real = a.real + b.real; 
    this->imaginary = a.imaginary + b.imaginary; 
    return *this; 
} 

ComplexNum& ComplexNum::diff(ComplexNum a, ComplexNum b) 
{ 
    this->real = a.real - b.real; 
    this->imaginary = a.imaginary - b.imaginary; 
    return *this; 
} 

ComplexNum& ComplexNum::prod(ComplexNum a, ComplexNum b) 
{ 
    this->real = (a.real * b.real) - (a.imaginary * b.imaginary); 
    this->imaginary = (a.real * b.imaginary) + (b.real * a.imaginary); 
    return *this; 
} 

ostream& ComplexNum::print(ostream& stm) const 
{ 
    return stm << "(" << noshowpos << real << showpos << imaginary << "i)"; 
} 

int main() 
{ 
    ComplexNum a, b; 
    cout << "First Complex Number:" << endl; 
    a.getComplexNum(); 
    cout << endl; 
    cout << "Second Complex Number:" << endl; 
    b.getComplexNum(); 
    cout << endl; 
    cout << fixed << setprecision(2) 
     << "a == " << a << '\n' 
     << "b == " << b << '\n' 
     << "a+b == " << a + b << '\n' 
     << "a-b == " << a - b << '\n' 
     << "a*b == " << a*b << '\n' 
     << "a*a == " << a*a << '\n' 
     << "b*b == " << b*b << '\n'; 
     cout << "a*a (using postincrement) == " << a++ << '\n'; //works fine 
     cout << "a*a (using preincrement) == " << ++a << '\n'; //gives me a [more than one operator error] underneath "++" 
     cout << endl; 

    system("PAUSE"); 
} 
+0

也許是因爲你有兩個預增量運營商?它應該怎麼知道要打電話給誰? – immibis

+2

您的「//對於num ++的朋友」和「//對於++的朋友」具有完全相同的簽名。所以他們中的一個在說謊。 – tkausl

+0

@tkausl,我的印象是,不同的回報隱含着不同的簽名。我錯了。謝謝你澄清這一點。這是否意味着我需要改變裏面//朋友的參數爲++ num?再次感謝。 – garyoak

回答

0

非會員前增量簽名

T& operator++(T& a); // ++a 

和非會員後增爲

T operator++(T& a, int); // a++ 

您必須更改這些定義來

friend ComplexNum& operator++(ComplexNum& a, int) { return a++; } //friend for num ++ 
friend ComplexNum operator++(ComplexNum& a) { return ++a; } //friend for ++num 
+0

我根據你的建議在我的班級裏修好了朋友符號。但是,現在我在主內部的'++ a'和'a ++'下面發生錯誤,並再次說'多個運算符'++'匹配這些操作數'。但是,'friend'和'overload'下面的紅線已經解決。 – garyoak

+0

更具體地說,它給了我'C2593:'操作符++'不明確' – garyoak

+0

因此,我將兩個朋友中的'ComplexNum&a'換成了'const ComplexNum&a',它做了竅門,但是現在我得到一個運行時錯誤,複數「a」,將其平方,然後再將其平方。當它應該給我同樣的答案。 'a ++'和'++ a'應該是相同的答案,也就是說。 – garyoak