操作员无匹配==错误,C
No match for operator == error, C++
这真的让我感到困惑。我正在努力超载C 中的比较运算符,并且我遇到了一个奇怪的错误,我不确定如何更正。
我正在使用的代码看起来像这样:
bool HugeInt::operator==(const HugeInt& h) const{
return h.integer == this->integer;
}
bool HugeInt::operator!=(const HugeInt& h) const{
return !(this == h);
}
其中 integer
是 short [30]
==
重载正常。但是,当我尝试在!=
主体中使用它时,它告诉我==
尚未定义。我是C 的新手,因此欢迎任何提示。
谢谢!
您正在尝试比较指针和实例。 this
是指向当前对象的指针,您需要先解释。
无论如何,您已经提到integer
是一系列短裤。这可能意味着您不应该将其与 ==
进行比较 - 您应该手动比较所有元素(当然,当然,您可以检查阵列中的元素数量是否相同,以防它们可以部分填充)。或者,您可以按照卢切安建议使用vector
- 它具有明确定义的operator==
。
这样(缺少星号)。
bool HugeInt::operator!=(const HugeInt& h) const{
return !(*this == h);
}
this
具有类型的HugeInt*
,但您就会使用它,就好像它是HugeInt&
。
而是使用return !(*this == h);
:)
bool HugeInt::operator!=(const HugeInt& h) const{
return !(this == h);
}
应该是
bool HugeInt::operator!=(const HugeInt& h) const{
return !(*this == h);
}
另外,如果integer
为short[30]
型,则比较将无法执行您的期望。如果您想要的话,您需要按元素比较元素。
另外,我建议使用std::vector<short>
代替原始数组?
超载运算符在对象上工作,而不是指针(像这样)。您应该在不平等运营商中解释这一点:
return !(*this == h);
您可以像以下方式结构它:
return( !operator==( h ) );
相关文章:
- 为什么我的作业操作员给出了损坏错误
- 操作员=在两个std :: chrono :: time_point引起错误之间
- 无操作员错误代码
- 在定义C2679和C2678错误代码的地方未找到二进制操作员
- 使用流操作员构建错误
- 错误:“操作员^”无匹配
- 使用多个操作员与操作员重载会产生奇怪的错误
- 使用%操作员时双重错误
- 错误:“ const char*”类型的操作数和“ const char [7]”类型为二进制“操作员 ”
- 新的操作员帮助,类型分配器和其他错误
- 学习OOP:超载 OR =操作员错误
- 为什么我无法使错误操作员()无法超载
- 通过使用新操作员创建对象会导致未解决的外部符号错误C FTGL
- &lt;&lt;操作员在C 中超载错误
- 使用单词变化C 不是操作员时的奇怪错误
- 在模板类中专门定义错误的朋友操作员
- C 模板错误无匹配操作员过载
- 超载流提取操作员错误
- 操作员无匹配==错误,C
- 在尝试超载&lt;&lt;时链接错误2005和1169操作员