Home > Back-end >  A friend function of nested template class
A friend function of nested template class

Time:09-27

I wrote a nested class templates, the outer and inner class has a class reloading operator } 'and' const hy: : List Compile error: : Node '), but if I will be nested inside and outside two classes to level, also is the inner class implements to the outer class, compile and execute such is no problem, hope to help me to look at problems, which is a great god code is simplified from the original code, I but the reported problem is the same, thank you!
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: : List list;
List. SetNode (5566);
STD: : cout & lt;
return 0;
}

CodePudding user response:

1., class Node outside the class definition, changed to: template STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const T & amp; The node)

 
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:

refer to the second floor cloudblaze response:
if or not, if you so change, there will be a compile-time error, can not access the private fields,


I have called you delete friend statement???????

CodePudding user response:

You could change your code to cut a figure or a post, thank you, may not understand what you're saying,

CodePudding user response:

I took
 
Template
The class List
{
Private:
The class Node
{
Private:
T _Data while forming;
Public:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Next:c
  • Related