2016-03-03 77 views
2

我在理解這個概念時遇到了一些問題。在main.cpp中的文件,我們有一個功能如下:在超載C++運算符時遇到問題

void TestComparison() 
{ 
    MyFloat X, Y; 

    cout << "\n\n============ Testing \"==\" for MyFloat ================\n"; 

    do 
    { 
     cout << "\nEnter X ==> "; 
     X.Read(); 
     cin.ignore(1000, '\n');    // Discard all chars in input stream. 
     cout << "\nEnter Y ==> "; 
     Y.Read(); 
     cin.ignore(1000, '\n');    // Discard all chars in input stream. 

     cout << "\n\n"; 

     if (X == Y) 
     { 
      X.Write(); cout << " is equal to "; Y.Write(); 
     } 
     else 
     { 
      X.Write(); cout << " is NOT equal to "; Y.Write(); 
     } 
    } 
    while (SpaceBarToContinue()); 
} 

這是我寫的類:

class MyFloat 
{ 
    enum {MAXDIGIT=20}; 
    char Number[MAXDIGIT+1]; 
    char NumberOfDigits; 

public: 

    friend void AssignValue(MyFloat& X);//remove after the program works 

    MyFloat(); 

    int Digits(); 
    int MaxDigits(); 
    void Read(); 
    void Write(); 

    MyFloat operator + (MyFloat x); 
    int operator== (MyFloat x); 
}; 

這裏是我的==過載保護功能存根:

int MyFloat::operator== (MyFloat x) 
{ 
    int Flag=0; 

    return 1; 
} 

這樣做的唯一目的是比較兩個對象X和Y的數組。它們被傳遞給==重載函數。我應該編寫比較它們的算法。我知道如何編寫比較這兩個字符數組的算法,那不是問題,但我不明白的是X和Y如何進入重載函數來比較它們?主要代碼(X == Y)用於獲得0或1. X和Y如何傳入函數?

舉例來說,我會承擔我的函數存根需要用2個參數被改寫:

int MyFloat::operator== (MyFloat x, MyFloat y) 
{ 
    int Flag=0; 

    return 1; 
} 

但這樣做的(X == Y)函數調用在此產生一個錯誤,回到主各國「超載「 operator ==「必須是一個二元運算符(有3個參數)'

所以我完全困惑於如何將MyFloat的兩個對象放入函數中來比較它們。我對編程還很陌生(5-6個月的學習),任何簡單明瞭的答案都將不勝感激。

+1

'==操作符'真的應該返回'bool',而不是'int'。 – PaulMcKenzie

+0

[重載'運算符+'的可能重複必須是一元運算符或二元運算符錯誤](http://stackoverflow.com/questions/13554320/overloaded-operator-must-be-a-unary-or-binary-operator-error ) – FredMaggiowski

+1

看看這個答案:http://stackoverflow.com/questions/13554320/overloaded-operator-must-be-a-unary-or-binary-operator-error 你已經聲明該操作符爲一個類成員,因此它接受一個隱式成員。你應該實現它作爲類的非成員MyFloat – FredMaggiowski

回答

3

當你寫:

if(a == b) 

它真正的意思是:

if(a.operator==(b)) 

所以在你的方法:

bool MyFloat::operator==(const MyFloat &x) const 
{ 
    // x is b in call above 
    // (*this) is a in call above 

    // Your class invariant should guarantee this: 
    // assert(x.NumberOfDigits < MAX_DIGITS); 

    // In the scope of your class' methods: 
    // NumberOfDigits corresponds to this->NumberOfDigits 
    // Number corresponds to this->Number 
    if(x.NumberOfDigits != NumberOfDigits) return false; 
    // Same as: if(x.NumberOfDigits != this->NumberOfDigits) return false; 
    return strncmp(x.Number, Number, NumberOfDigits) == 0; 
    // Same as: return strncmp(x.Number, this->Number, this->NumberOfDigits) == 0; 
} 

注意我改變了signatu你的方法的重新。正確的簽名返回bool並將const(因爲您不想更改參數)引用(避免複製大對象)作爲參數。該方法是(並且必須)const,因爲它不應該修改對象,並且它必須可以在const對象上調用。

注意,就可以定義操作符作爲非成員函數(即類外)具有以下特徵:

bool operator==(const MyFloat &a, const MyFloat &b) 
+0

再對!你是一個非常好的評論者:-) –

+1

多年的實踐。我只會提出的問題是斷言,但那是一個意識形態。在構造或設置時,應該測試無效大小並拒絕它,以便該對象永遠不會處於非法狀態,並且您不必對get進行測試。通常比集合更多。 – user4581301

+0

是的,我猶豫之間把它放在真實或僅僅作爲一個評論。我也不太喜歡assert的用法,它只是強調類不變,以及'strncmp'不需要風險的事實。 –

-1

成員函數(包括重載運算)有一個隱含的this參數傳入。在你的情況,因爲你正在使用的operator==成員的版本,你應該只需要一個參數,另一個是this

+0

降低選民照顧評論。我該如何改進這個答案? – YoungJohn

+0

我不是downvoter,但也許你的答案已被低估,因爲它可以改進,例如代碼或一些參考。順便說一下,這是一個工作的解決方案,因此這是非常不公平的: – FredMaggiowski

+0

只是爲了澄清,我沒有投下。 –

2

您應該使用this指針。欲瞭解更多信息:Source

bool MyFloat::operator==(const MyFloat& x) const 
{ 
    for(int i = 0; i < x.MaxDigits; ++i) 
    { 
     if(x[i] != (*this)[i]) 
      return false; 
    } 
    return true; 
} 
+0

我認爲你的意思是說'this [i]'not'this.x [i]'as 'MyFloat'沒有成員'x' – callyalater

+1

不處理大小不同的'MyFloats'並且語法不正確的情況,幾乎就是你的想法 – user4581301

+0

它只是給出了一個想法來改進代碼片段 – snr