List. H:
# # ifndef LIST_H
# define LIST_H
# include & lt; iostream>
The namespace hy
{
Template
The class List
{
Private:
The class Node
{
Private:
T _Data while forming;
Public:
The Node ();
The Node (const Node & amp; The node);
The Node & amp; Operator=(const Node & amp; The node);
~ the Node ();
Void SetValue (const T & amp; Item);
Template
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const typename List: : Node & amp; The node);
};
Private:
List: : Node _Node;
Public:
The List ();
The List (const List& The list);
List& Operator=(const List & The list);
To the List ();
Void SetNode (const T & amp; Item);
Template
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const List& The list);
};
Template
List: : Node: : Node ()
{
}
Template
List: : Node: : Node (const List : : Node & amp; The node)
{
_Data while forming=https://bbs.csdn.net/topics/node._Data;
}
Template
Typename List: : Node & amp; List : : Node: : operator=(const List : : Node & amp; The node)
{
If (this==& amp; The node)
{
return *this;
}
_Data while forming=https://bbs.csdn.net/topics/node._Data;
return *this;
}
Template
List: : Node: : ~ Node ()
{
}
Template
Void List: : Node: : SetValue (const T & amp; Item)
{
_Data while forming=https://bbs.csdn.net/topics/item;
}
Template
STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const typename List: : Node & amp; The node)
{
OS & lt;Return the OS;
}
Template
List: : List ()
{
}
Template
List: : List (const List & The list)
{
_Node=list. _Node;
}
Template
List& List : : operator=(const List & The list)
{
If (this==& amp; The list)
{
return *this;
}
_Node=list. _Node;
return *this;
}
Template
List: : ~ a List ()
{
}
Template
Void List: : SetNode (const T & amp; Item)
{
_Node. The SetValue (item);
}
Template
STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const List& The list)
{
OS & lt;Return the OS;
}
}
# endif
The main. CPP:
# include & lt; iostream>
# include "List. H"
Int main ()
{
Hy: : Listlist;
List. SetNode (5566);
STD: : cout & lt;
return 0;
}
CodePudding user response:
1., class Node outside the class definition, changed to: template
Template
STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const T & amp; The node)
{
OS & lt;Return the OS;
}
Template
STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const List& The list)
{
OS & lt;: : Node, guess guess guess when looking for function matching no longer tube outside class List Match,
Return the OS;
}
2. Class, Node class directly define friend operator<, you can compile by:
The class Node
{
Private:
T _Data while forming;
Public:
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, Node const & amp; The node) {
}
};
CodePudding user response:
Like or not, if you so change, there will be a compile-time error, can not access the private fields,CodePudding user response: