I am implementing a binary search tree for a school project wherein I need a bst class, an iterator class, and a node class. The project is such that all of these classes must be defined in a .h file, meaning that declarations and definitions are, at least in my interpretation, the same.
The skeleton of this code looks like this:
template<typename T, typename C = std::less<T>>
class bst {
public:
class node {
private:
node* left;
node* right;
node* parent;
T value;
C pred;
friend class bst;
friend class iterator;
public:
(...) //constructor, destructor, functions that have no issues...
};
class iterator {
private:
node* address;
const bst* container;
friend class bst;
public:
(...) // constructor, destructor, other stuff.
// now, the problem causers:
friend bool operator<(const iterator&, const iterator&);
// Declare and define equals and not equals operators.
friend bool operator==(const iterator&, const iterator&);
friend bool operator!=(const iterator&, const iterator&);
// Declare and define dereferencing and arrow operator.
const T& operator*() const { return this->address->value; }
const T* operator->() const { return &this->address->value; }
};
friend bool operator<(const iterator& it1, const iterator& it2) {
return pred(it1.address->value, it1.address->value);
}
friend bool operator==(const iterator& it1, const iterator& it2) {
if (!(it1.address->value < it2.address->value) &&
!(it2.address->value < it1.address->value)) {
return true;
}
else { return false; }
}
friend bool operator!=(const iterator& it1, const iterator& it2) {
return !(it1.address->value == it2.address->value);
}
(...) // bst rule of 5 constructors, destructor, functions ...
};
So my issue is in the friend bool operator definitions, namely that they cannot seem to access the private member variable @param value defined in node. I would have thought this would be avoided by declaring bst and iterator as friend classes in node, but I cannot seem to get around the issue. I read somewhere that the problem might have been caused by missing the rule of 5 in the iterator class, so I tried implementing all of the appropriate rule of 5 constructors and operators there to no avail. Help!