我早些时候在这里获取有关重载 *= 的一些信息,发现它非常有帮助。
我现在正试图重载 == 和 !=。但我想让运算符函数成为非成员函数。
尽量不要讨厌我所有的代码块——我已经包含了我认为与问题相关的内容。
在我的源代码中,我声明了函数:
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)'
经过大量的互联网研究,我仍然不知道这些错误是什么意思。
帮助?谢谢!