Ben*_*ach 2 c++ operator-overloading
这真让我烦恼.我正在使用C++重载比较运算符,我得到一个奇怪的错误,我不知道如何纠正.
我正在使用的代码如下所示:
bool HugeInt::operator==(const HugeInt& h) const{
return h.integer == this->integer;
}
bool HugeInt::operator!=(const HugeInt& h) const{
return !(this == h);
}
Run Code Online (Sandbox Code Playgroud)
这里integer
是一个short [30]
该==
超载工作正常.但是当我尝试在!=
身体中使用它时,它告诉我==
尚未定义.我是C++的新手,所以欢迎任何提示.
谢谢!
Iva*_*iev 12
您正在尝试比较指针和实例.this
是指向当前对象的指针,您需要先取消引用它.
无论如何,你提到过这integer
是一系列短裤.这可能意味着你不应该将它与之比较==
- 你应该手动比较所有元素(当然,在你检查数组中元素的数量是否相同之后,以防它们可以部分填充).或者你可以使用vector
Luchian建议的 - 它有一个明确的定义operator==
.
像这样(缺少星号).
bool HugeInt::operator!=(const HugeInt& h) const{
return !(*this == h);
}
Run Code Online (Sandbox Code Playgroud)