2013-09-30 44 views
1

我剛纔在這裏得到一些關於重載* =的信息,並發現它非常有幫助。Overloading == and!=

我現在想重載==和!=。但是我想讓運營商功能非會員功能。

儘量不要討厭我所有的代碼塊 - 我已經列入了我認爲與問題相關的內容。

在我的源代碼,我宣佈的功能:

bool operator==(const Statistician&, const Statistician&); 
// checks whether Stat1 == Stat2 
bool operator!=(const Statistician&, const Statistician&); 
// checks whether Stat1 != Stat2 

後來我定義的功能:

// Check if equal 
bool operator ==(const Statistician& Stat1, const Statistician& Stat2) 
{ 
if ((Stat1.get_length() == Stat2.get_length()) && 
    (Stat1.get_sum() == Stat2.get_sum()) && 
    (Stat1.get_mean() == Stat2.get_mean()) && 
    (Stat1.get_minimum() == Stat2.get_minimum()) && 
    (Stat1.get_maximum() == Stat2.get_maximum())) 
    return true; 
else 
    return false; 
} 

// Check if not equal 
bool operator !=(const Statistician& Stat1, const Statistician& Stat2) 
{ 
if ((Stat1.get_length() != Stat2.get_length()) && 
    (Stat1.get_sum() != Stat2.get_sum()) && 
    (Stat1.get_mean() != Stat2.get_mean()) && 
    (Stat1.get_minimum() != Stat2.get_minimum()) && 
    (Stat1.get_maximum() != Stat2.get_maximum())) 
    return true; 
else 
    return false; 
} 

在我的測試車手,我做實際測試:

if (Stat1 == Stat2) {cout << "Equal";} 
if (Stat1 != Stat2) {cout << "Not Equal";} 

我的代碼的其餘部分工作正常,這些都不是類成員函數。

但是我編譯時得到這些錯誤:

error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Statistician' (or there is no acceptable conversion) 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(507): could be 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(512): or  'bool std::operator ==(std::nullptr_t,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(517): or  'bool std::operator ==(const std::exception_ptr &,std::nullptr_t)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(426): or  'bool std::operator ==(const std::error_code &,const std::error_condition &) throw()' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(434): or  'bool std::operator ==(const std::error_condition &,const std::error_code &) throw()' 
     while trying to match the argument list '(Statistician, Statistician)' 

error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'Statistician' (or there is no acceptable conversion) 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(522): could be 'bool std::operator !=(const std::exception_ptr &,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(527): or  'bool std::operator !=(std::nullptr_t,const std::exception_ptr &)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\exception(532): or  'bool std::operator !=(const std::exception_ptr &,std::nullptr_t)' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(443): or  'bool std::operator !=(const std::error_code &,const std::error_condition &) throw()' 
     c:\program files (x86)\microsoft visual studio 11.0\vc\include\system_error(450): or  'bool std::operator !=(const std::error_condition &,const std::error_code &) throw()' 
     while trying to match the argument list '(Statistician, Statistician)' 

很多互聯網研究後,我仍然不知道這些錯誤的意思。

幫助?謝謝!

+1

在您第一次使用它們之前,您是否聲明瞭這些操作員? – us2012

+3

請注意,你不需要做'如果(某些)返回true;否則返回false;',你可以說'return something;'。 – juanchopanza

回答

5

首先,你operator!=是錯誤的,它應該是

if ((Stat1.get_length() != Stat2.get_length()) || 
    (Stat1.get_sum() != Stat2.get_sum()) || 
    (Stat1.get_mean() != Stat2.get_mean()) || 
    (Stat1.get_minimum() != Stat2.get_minimum()) || 
    (Stat1.get_maximum() != Stat2.get_maximum())) 
    return true; 
else 
    return false; 

(注意:||而不是&&)。更重要的是,使該

return !(Stat1==Stat2); 

現在你看到的錯誤:它看起來像你忘了包括在您使用它們的翻譯單元聲明這些運營商的頭。或者,如果情況並非如此,您需要檢查它們所在的名稱空間,以及它們是否可以從被調用的位置找到它們。

只是爲了檢查:如果你說「在我的源代碼中,我聲明瞭這些函數:」你的意思是頭文件(*.h)和「稍後」是指實現文件(*.cc),對不對?

+0

+1還將'operator =='的定義簡化爲'return(Stat1.get_length()== Stat2.get_length())&& ...;'。就個人而言,我會讓運營商的朋友,並使用'std :: tie'來執行比較。 – Praetorian

+1

@Praetorian對於有經驗的開發人員,你是對的,但鑑於OP顯然剛剛開始瞭解運營商,讓我們一步一步,不要因爲太多的信息而殺死她:) –

+0

因此,對於那個錯誤,我有函數聲明以及相同.cpp文件中的定義。所以不應該有這個問題。 它說,沒有哪個運營商需要統計員爲左手輸入,然而當我宣佈我的功能聲明爲 (統計員和,統計員和) 所以我很失去了現在。 – 404Cat