I’m trying to overload the << operator using a friend function, but it’s not seeing the private member variables for some reason. Any ideas why this is happening would be quite helpful.
Here’s the header file
class Set
{
private:
struct Node
{
int val;
Node* link;
};
Node *cons(int x, Node *p);
Node *list;
public:
Set() {list = NULL;}
bool isEmpty() const;
int size() const;
bool member (int x) const;
bool insert (int x);
bool remove (int x);
void print() const;
const Set operator+(const Set &otherSet)const;
const Set operator*(const Set &otherSet)const;
Node* getSet()const{return list;}
void setList(Node *list);
friend ostream& operator <<(ostream& outputStream, const Set &set);
};
Here’s the function definition.
ostream& operator <<(ostream& outputStream, const Set &set)
{
Node *p;
p = list;
outputStream << '{';
while(p->link != NULL)
{
outputStream << p->val;
p = p->link;
}
outputStream << '}';
return outputStream;
}
The problem is not with the accessibility of
Nodebut rather with its scope: the unqualified type name does not become in scope through friendship – you should useSet::Nodeinstead.Same goes for the
listvariable: it should beset.list.With these two changes in place, your code compiles fine on ideone.