我正在尝试使用友函数重载<
这是头文件
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);
};
这是函数定义。
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;
}
最佳答案
问题不在于Node
的可访问性,而在于它的范围:不合格的类型名称不会通过友谊变成作用域-您应该改用Set::Node
。list
变量也是如此:应该为set.list
。
完成这两个更改后,your code compiles fine on ideone。