c++ - 重载 == 和 !=

标签 c++ operator-overloading overloading

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

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

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

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

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

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

最佳答案

首先你的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);

现在您看到的错误是:您似乎忘记在使用它们的翻译单元中包含声明这些运算符的 header 。或者,如果不是这种情况,您需要检查它们位于哪些 namespace 中,以及是否可以从调用它们的地方找到它们。

只是检查一下:如果您说“在我的源代码中,我声明了函数:”您指的是头文件 (*.h),而“稍后”指的是实现文件 (*.cc),对吧?

关于c++ - 重载 == 和 !=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103080/

相关文章:

c++ - 部分特化模板成员函数

c++ - boost shared_ptr 和派生类

c++ - 对 operator+ 和/或 operator+= 使用 move 语义有意义吗?

java - 可变参数和重载的错误?

c# - 动态转换问题

c++ - 来自 GCC 内部的运算符 '>' 或 '>>' bug 需要模板参数 C++ 中的表达式

c++ - 在 C++ 中传递 C 字符串的问题

typescript - typescript 中的重载函数类型

c# - 为什么不能在静态类中重载运算符?

c++ - 从基本数据类型到用户定义类型的类型转换