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?

0

2 Answers 2

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;
}
5
  • Do you have any idea why I would get this error?? Stack<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Node::Node(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)' referenced from C:\USERS\JOHNNY\DESKTOP\STACK\INFIX_TO_RPN.OBJ
    – Johnrad
    Nov 1, 2010 at 1:18
  • I presume it's a link time error, because the Node constructor is undefined (or not part of the build)? Nov 1, 2010 at 1:21
  • My node class is a private member of my stack class.
    – Johnrad
    Nov 1, 2010 at 1:24
  • But, does the Node constructor have a definition? Is that definition (i.e., if it's in a .cpp file) part of your build (i.e., link) ? Nov 1, 2010 at 1:25
  • Yes it is a part of my build. It has been defined.
    – Johnrad
    Nov 1, 2010 at 1:27
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;
}
8
  • 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? Nov 1, 2010 at 1: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. Nov 1, 2010 at 1:18
  • Consider what would happen if both the source stack and the destination stack are very large (Hint: 3 > 2) Nov 1, 2010 at 1:19
  • @Michael: I have considered that. Have you read the article to which I linked? Nov 1, 2010 at 1: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. Nov 1, 2010 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.