I realize I’m stupid so please go easy on me. Yes this is an assignment, but I want to understand what I’m doing wrong, not just the answer.
I’m trying to write an operator== and an operator!= function for an iterator on a binary search tree in the BSTIterator class template.
Given (all in the same class template):
private:
BSTNode<Data>* curr;
...
bool operator==(BSTIterator<Data> const & other) const {
(here's where I do my magic)
}
Same setup for operator!=.
I write for == …
return (&curr == other);
I don’t think I need the parentheses but anyway … here’s what I have for !=
return !(&curr == other);
My compiler has a problem with != but not apparently ==.
It spit out a lot of gobbledy gook but, as far as I can tell, the relevant part is:
No match for ‘operator!=’ in ‘&((const
BSTIterator*)this)->BSTIterator::curr != other’
and it references the line that says return !(&curr == other);
I think at first the compiler didn’t like my operator== function either but I see no reference to it now. Why would it like one and not the other when they are basically the same except for the ! ?
Please let me know if I need to include more information.
As I understand from your code
currindicate the position that your iterator point to it.Do when you compare an instance of your iterator with another you should check if they both point to the same location. am I right? if the answer is true shouldn’t you code like this:
And about your error: your compiler say
&curis of a type(BSTNode<Data>**) that I don’t know how to compare it with an instance of your iterator(BSTIterator<Data>) and that’s obvious since you are defining==and!=for your class and you never defined and operator for such operation, do you?