2

我早些时候在这里获取有关重载 *= 的一些信息,发现它非常有帮助。

我现在正试图重载 == 和 !=。但我想让运算符函数成为非成员函数。

尽量不要讨厌我所有的代码块——我已经包含了我认为与问题相关的内容。

在我的源代码中,我声明了函数:

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)'

经过大量的互联网研究,我仍然不知道这些错误是什么意思。

帮助?谢谢!

4

1 回答 1

6

首先,你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),对吧?

于 2013-09-30T20:24:29.073 回答