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: Header File Pastebin
Here is the driver program: Driver Program

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: Output

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. 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.

In general assignement should not be done if you are trying to assign something to itself. If you aren't then you want copy data from the rhs to the lhs in the same order in rhs. One way to do that is to copy the data in the rhs list from head to tail into the lhs list, always adding to the tail. Alternativey you could pop each data member from rhs and push in on a temp stack, one at a time. When all data has been transfered into the temp stack, then you could pop it off the temp stack onto lhs to get the data in the same order as in rhs.

You can do pretty much the same thing with the << operator----use the embedded list to ouput the data. Alternatively, after creating a temp list you could display the value of the popped entry at the same time as you push it back onto the stack.

The implementation of the process is less important than maintaining the interface. That is, the assignment operator and the output operator must do what they always do.

Okay, I think I understand what you mean. When you say from head to tail, I don't have a tail pointer in the code, only a head pointer. I think having a tail means that it would be a doubly-linked list, no? Anyway you could show me a sample of the code so I can learn from it and implement both operators?

Thanks for your help.

Ok, here's what I tried for the output operator.

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

However when I try to compile it, it tells me that head is undeclared (first use this function). I already declared it under private: in the class stack though, so why isn't it working? Also, does that code look ok to output the data?

Thanks again.

The problem with head is that the << operator is called using the stream, not rightOp, so what head head is supposed to be used. ther head of rightOp, bu how is the compiler going to know if you don't tell it.

Can't say that I write a lot of templated code so how that is going to affect everything I can't say. But, the general strategy seems okay to me.

I'm still not really sure what changes I should make.

can you give me an example program that illustrates the linked lists and has two operators?

can you give me an example program that illustrates the linked lists and has two operators?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.