我正在尝试重载 ostream << 运算符class List
class Node
{
public:
int data;
Node *next;
};
class List
{
private:
Node *head;
public:
List() : head(NULL) {}
void insert(int d, int index){ ... }
...}
据我所知(重载 ostream 函数)必须在类之外编写。所以,我这样做了:
ostream &operator<<(ostream &out, List L)
{
Node *currNode = L.head;
while (currNode != NULL)
{
out << currNode->data << " ";
currNode = currNode->next;
}
return out;
}
但是当然,这不起作用,因为成员Node head
是private
. 在这种情况下,除了转向,还有哪些方法可以Node *head
做public
?