2

I am currently working on a stack that implements linked list. I am having a problem when it comes to overloading the "=" operator. I am very clueless as to what to do. If anyone could point me in a good direction that would be awesome.

//operator overload
template <class S>
const Stack<S>::operator=( const Stack& s )
{

    if (s.isEmpty())
        theFront = theTop = 0
    else
    {
        NodePointer temp = q->theFront;

        while(temp != 0)
        {
            push(temp->data);
            temp = temp->next;
        }
    }

    return *this;
}

I am also getting this error : Stack, std::allocator > >::Node::Node(std::basic_string, std::allocator >)' referenced from C:\USERS\JOHNNY\DESKTOP\STACK\INFIX_TO_RPN.OBJ

Can this be fixed by my operator overload function?

Johnrad
  • 2,637
  • 18
  • 58
  • 98

2 Answers2

2

You need to empty the current stack, before pushing data on it. You should add a removeAll function, and call it at the top of the assignment (after a check for self assignment, which is also a good idea). Otherwise, it looks correct. So, the end result would be:

//operator overload 
template <class S> 
const Stack<S>::operator=( const Stack& s ) 
{ 
    // Check for self assignment
    if (&s==this)
        return *this;

    // Clear the current stack
    removeAll();

    // Copy all data from stack s
    if (!s.isEmpty())
    { 
        NodePointer temp = q->theFront; 

        while(temp != 0) 
        { 
            push(temp->data); 
            temp = temp->next; 
        } 
    } 

    return *this; 
} 

Here is a sample removeAll function:

template <class S> 
void Stack<S>::removeAll()    
{ 
    while (s.theFront)
    {
        NodePointer p = s.theFront;

        s.theFront = s.theFront->next;
        delete p;
    }

    s.theTop = s.theFront;
}
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
1

Instead of manually implementing the copy assignment operator for your class, use the copy-and-swap idiom.

Once you've implemented a swap() function for your class (the article to which I linked above provides an excellent description of how to do this), the operator= overload becomes short and simple:

Stack& operator=(Stack rhs)
{
    swap(rhs);
    return *this;
}
Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • You do realize that by passing rhs by value, you will be deep copying the right hand side with each assignment, even if there is potentially a gain to be had here with exception safety? – Michael Goldshteyn Nov 01 '10 at 01:15
  • @Michael: You have to make a copy. You have a choice between taking `rhs` by value and having the copy made in the function call, or taking `rhs` by const reference and making the copy yourself in the body of the assignment operator. The compiler may frequently be able to eliminate the former copy; it all but certainly can't elide the latter. – James McNellis Nov 01 '10 at 01:18
  • Consider what would happen if both the source stack and the destination stack are very large (Hint: 3 > 2) – Michael Goldshteyn Nov 01 '10 at 01:19
  • @Michael: I have considered that. Have you read the article to which I linked? – James McNellis Nov 01 '10 at 01:20
  • It's not an article, it's a pointer to an SO question with links that are too small (to humanly read) and answers that are too many to "gloss over." It was at this point that I totally lost interest. – Michael Goldshteyn Nov 01 '10 at 01:22
  • @Michael: The first answer to that question is an article describing the copy-and-swap idiom in detail and explains why it is the correct solution to this problem. The approach that you present is wrong because it is not exception-safe. – James McNellis Nov 01 '10 at 01:28
  • How will this exception safe approach handle a bad_alloc in the middle of the construction of the (large) temporary stack, upon which the correct thing to do would be to terminate the application? An exception that could have been wholly avoided with two (large) stacks instead of three. – Michael Goldshteyn Nov 01 '10 at 01:31
  • And this is exactly what my code did. You can add more exception safety to what I did, but the addition of the third stack is not a good solution IMHO. – Michael Goldshteyn Nov 01 '10 at 01:46