3

I have made a Class template to implement a "node-based" Stack named as "mystack" -:

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;
}  

But what i actually want is that the struct mystack_node shouldn't be accesible to any other part of the code except for the class mystack. So I tried the following workaround -:

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;
}  

But i get the following error from the compiler -:

In function ‘std::ostream& operator<<(std::ostream&, const mystack<T>&)’:  
error: ‘temp’ was not declared in this scope  

Could anybody please tell how to fix this problem??

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77
  • 3
    You need the keyword `typename` in front of the declaration of `temp`: `typename mystack::mystack_node *temp = a.stack_top;` – jogojapan Jul 26 '13 at 06:48
  • 1
    Similar question: http://stackoverflow.com/questions/1301380/g-is-not-a-type-error – jogojapan Jul 26 '13 at 06:50
  • 1
    @jogojapan you should make that the answer to the question. – Pete Jul 26 '13 at 07:54
  • @Pete My secret plan was to wait until the questioner confirms this is truly the problem, and then to vote to put it on hold as a duplicate. There must be hundreds of posts with this problem. (But answering it isn't wrong. Anyone, feel free to post an answer here.) – jogojapan Jul 26 '13 at 08:02
  • @jogojapan This really did solve the problem... and if you feel it to be a duplicate of the mentioned question, you are free to mark it one. – Anmol Singh Jaggi Jul 26 '13 at 08:13

1 Answers1

2

As mentioned in the comments, I needed to insert the keyword typename in front of the declaration of temp -:

typename mystack<T>::mystack_node *temp = a.stack_top;

Anmol Singh Jaggi
  • 8,376
  • 4
  • 36
  • 77