Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符==错误,C+;不匹配+;_C++_Operator Overloading - Fatal编程技术网

C++ 运算符==错误,C+;不匹配+;

C++ 运算符==错误,C+;不匹配+;,c++,operator-overloading,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); } 其中,inte

这真让我烦透了。我正在研究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++新手,所以欢迎使用任何提示。
谢谢

您正在尝试比较指针和实例<代码>此
是指向当前对象的指针,您需要先解除对它的引用


无论如何,您已经提到了
integer
是一个短数组。这可能意味着您不应该将其与
==
进行比较-您应该手动比较所有元素(当然,在检查数组中的元素数量是否相同之后,以防它们可以部分填充)。或者你可以像Luchian建议的那样使用一个
向量
——它有一个定义良好的
操作符==

这个
有类型
HugeInt*
,但是你使用它的时候就好像它是
HugeInt&
一样

使用
返回!(*此==h)改为:)

像这样(缺少星号)

应该是

bool HugeInt::operator!=(const HugeInt& h) const{
    return !(*this == h);
}
此外,如果
integer
的类型为
short[30]
,则比较不会达到预期效果。如果需要的话,您需要逐个元素进行比较


另外,我是否可以建议使用
std::vector
而不是原始数组?

重载运算符处理对象而不是指针(如下所示)。您应该在不等式运算符中取消引用:

return !(*this == h);
您也可以将其构造为:

return( !operator==( h ) );

看一看这条类似的线,谢谢!这使它运行正常。它实际上是一个
short
s的数组;这有区别吗?这是我正在学习的一个类的一个小项目,看起来我们只是希望比较一个短数组和另一个短数组。任何更多的内容都可能超出类的范围(目前)。不,它也不会与
short
s一起工作-它只会检查它是否实际上是同一个数组。为了比较内容,您应该迭代并比较每个元素。
return !(*this == h);
return( !operator==( h ) );