我剛纔在這裏得到一些關於重載* =的信息,並發現它非常有幫助。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)'
很多互聯網研究後,我仍然不知道這些錯誤的意思。
幫助?謝謝!
在您第一次使用它們之前,您是否聲明瞭這些操作員? – us2012
請注意,你不需要做'如果(某些)返回true;否則返回false;',你可以說'return something;'。 – juanchopanza