J'ai fait un modèle de classe pour mettre en œuvre un « à base de noeud » Stack nommé comme "mystack"
-:C++: Champ d'struct dans une classe
template<typename T> class mystack;
template<typename T>ostream& operator <<(ostream& out,const mystack<T> &a);
template<typename T> struct mystack_node // The data structure for representing the "nodes" of the stack
{
T data;
mystack_node<T> *next;
};
template<typename T> class mystack
{
size_t stack_size; // A variable keeping the record of number of nodes present in the stack
mystack_node<T> *stack_top; // A pointer pointing to the top of the stack
/*
...
...(The rest of the implementation)
...
*/
friend ostream& operator << <T> (ostream&,const mystack&);
};
template<typename T>ostream& operator <<(ostream& out,const mystack<T> &a) // Output operator to show the contents of the stack using "cout"
{
mystack_node<T> *temp=a.stack_top;
while(temp!=NULL)
{
out<<temp->data<<" ";
temp=temp->next;
}
return out;
}
Mais ce que je veux réellement est que le struct mystack_node
shouldn » t être accessible à toute autre partie du code à l'exception de la classe mystack
. J'ai donc essayé la solution suivante -:
template<typename T> class mystack;
template<typename T>ostream& operator <<(ostream& out,const mystack<T> &a);
template<typename T> class mystack
{
struct mystack_node // The data structure for representing the "nodes" of the stack
{
T data;
mystack_node *next;
};
size_t stack_size; // A variable keeping the record of number of nodes present in the stack
mystack_node *stack_top; // A pointer pointing to the top of the stack
/*
...
...(The rest of the implementation)
...
*/
friend ostream& operator << <T> (ostream&,const mystack&);
};
template<typename T>ostream& operator <<(ostream& out,const mystack<T> &a) // Output operator to show the contents of the stack using "cout"
{
mystack<T>::mystack_node *temp=a.stack_top;
while(temp!=NULL)
{
out<<temp->data<<" ";
temp=temp->next;
}
return out;
}
Mais je reçois l'erreur suivante du compilateur -:
In function ‘std::ostream& operator<<(std::ostream&, const mystack<T>&)’:
error: ‘temp’ was not declared in this scope
Quelqu'un pourrait s'il vous plaît dire comment résoudre ce problème ??
Vous devez le mot-clé 'typename' devant la déclaration de' temp' : 'typename mystack :: mystack_node * temp = a.stack_top;' –
jogojapan
Question similaire: http://stackoverflow.com/questions/1301380/g-is-not-a-type-error – jogojapan
@jogojapan vous devriez faire cela La réponse a la question. – Pete