This is really bugging me. I'm working on overloading the comparison operators in C++, and I'm getting a weird error that I'm not sure how to correct.
The code I'm working with looks like this:
bool HugeInt::operator==(const HugeInt& h) const{
return h.integer == this->integer;
}
bool HugeInt::operator!=(const HugeInt& h) const{
return !(this == h);
}
where integer
is a short [30]
The ==
overloading works fine. But when I try to use it in the !=
body, it tells me that ==
has not been defined. I'm new to C++, so any tips are welcome.
You're trying to compare a pointer and an instance. this
is a pointer to the current object, you need to dereference it first.
Anyway, you've mentioned that integer
is an array of shorts. That probably means you shouldn't compare it with ==
- you should just compare all the elements manually (after, of course, you check that the number of elements in the arrays is the same, in case they can be filled partially). Or you could use a vector
as Luchian suggested - it has a well-defined operator==
.