Overloading operators in singly linked list (C++)

Hey everyone,

I'm working on an assignment and I'm having some trouble figuring out how to implement two operators. The program creates and implements a class to represent the Stack ADT using a singly-linked list. There is a driver program to test my implementation.

I have everything implemented (except the top() method) correctly as far as I can tell, but I am really struggling to implement operator= and operator<<. Here is the description for the two:


operator=: The assignment operator should be properly overloaded.

operator<<: The output operator should be overloaded so that an entire Stack can be sent to the standard output. As usual, this function will need to be a friend rather than a method. Declaring a template function to be a friend of a template class requires some special syntax.Declaring a template function to be a friend of a template class is one of the classic "gotcha's" in C++. We are trying to declare a function to be a friend of all classes that might be instantiated from the Stack template class, and most C++ compilers will quite properly refuse to do that without some special syntax. The friend declaration must contain an extra set of <> to indicate that it is a template function (however, do not code this in the actual function definition - only the friend declaration). You'll also usually need to forward declare both the template class and the template function


Here is my header file: http://pastebin.com/je1a0UJK
Here is the driver program: http://pastebin.com/wfqSR9w9

I tried to make my code as readable as possible and explain what I'm doing, let me know if you have questions about any of it.

And here is what the output should look like: http://pastebin.com/DV7gLZuy

Any help is greatly appreciated, I have been trying to figure it out for at least 4 hours now and have had nothing but errors.

Here is what I tried to do for the output operator but it tells me that I have not defined head (although it is defined under private: in the Stack class).

1
2
3
4
5
6
7
8
9
10
11
    template <class T>
    ostream& operator<<(ostream& leftOp, const Stack<T>& rightOp)
    {
    SNode<T>* ptr = head;
    while (ptr != NULL)
    {
    leftOp << ptr -> value << " ";
    ptr = ptr -> next;
    }
    return leftOp;
    }


Anyone that can help me get to understand how to do this would be a huge help. Also, any general improvement comments are welcome. Thanks.
Anyone know how to overload the << operator?
> Here is what I tried to do for the output operator but it tells me that I have not defined head
> (although it is defined under private: in the Stack class).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class T>
class Stack<T>
{     

    // ...

     friend ostream& operator<<( ostream& leftOp, const Stack<T>& rightOp )
    {
        //SNode<T>* ptr = head;
        SNode<T>* ptr = rightOp.head;
        while( ptr != 0 ) // while (ptr != NULL)
        {
             leftOp << ptr -> value << " ";
             ptr = ptr -> next;
        }
        return leftOp;
    }

    // ...

   private:
        Snode<T>* head ;
}
Topic archived. No new replies allowed.