Thread: SLList operator= overloading problems

  1. #1
    Codin huh?
    Join Date
    Dec 2004
    Posts
    6

    SLList operator= overloading problems

    Hi, ive been trying to get this operator= overload to work for a few days now..

    this is my recent attempt... Think you could help me out. Thanx


    Code:
    SLList<Type> &operator= (const SLList<Type> &list)
        {
            if(this == &list)
               return *this;
            SLList<Type> temp = list;
            
            return temp;
        }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you are trying to return a reference to a local variable which is a big no no.
    operator = should be setting the values of *this object to the values of list.

    So you'd have to look at the internals of SSList and do something like:
    Code:
      a = list.a;
      b = list.b;
      c = list.c;
    // etc

  3. #3
    Codin huh?
    Join Date
    Dec 2004
    Posts
    6
    Thanx, I understand that as far as basics, im not quite sure as to how, to apply this method, when working the "list" object, since its a dynamic SinglyLinked List;
    i think i said that right.

  4. #4
    Codin huh?
    Join Date
    Dec 2004
    Posts
    6
    or perhaps my error lies somewhere else. maybe this can help.


    Code:
    template <typename Type>
    class SLLIter;
    
    template <typename Type>
    class SLList
    {
    private:
        struct Node
        {
            Type element;
            Node *next;
        };
    
        Node *head;
    
    public:
        //SLList(void);
    friend class SLLIter;
       
    
        // SLList : constructor.
        SLList ()
        {
            head = NULL;
        }
       
        // operator = : assignment operator.
        // NOTE : invalidates all iterators to this list.
        SLList<Type> &operator= (const SLList<Type> &list)
        {
    
            //if(this == &list)
            //    return *this;
    
              
            if(this == &list)
               return *this;
            else
            {
            *this = list;
            return *this;
            }
        }
        // addHead : append a new item to the front of the list.
        void addHead (const Type &v)
        {
            Node *temp = new Node;
            temp->element = v;
            temp->next = head;
            head = temp;
        }
        
        // clear : clear out the list
        void clear ()
        {
            while(head)
            {
                Node *temp = head->next;
                delete head;
                head = temp;
            }
        }
        
        // insert : place a new item directly before the index 
        //          passed in.  Do nothing on a bad iterator.
        // Note : iterator will point at the newly inserted 
        // 	     item after the function call.
        void insert (SLLIter<Type> &index, const Type &v)
        {
            Node *temp = new Node;
            temp->element = v;
            temp->next = head;
            head = temp;
            
    
        }
        
        // remove : remove (and delete) the item at the index 
        //          passed in.  Do nothing on a bad iterator.
        // Note : iterator will point at the item after the one 
        // 	     removed after the function call, or NULL (bad) if 
        //        the tail is the one removed.
        void remove (SLLIter<Type> &index)
        {
    
    
    
            Node *cur, *prev;
            // walk through all list items.
            for (prev = NULL, cur = head; cur; prev = cur, cur = cur->next)
            {  
                if (cur->element == index.curr->element)
                {
                    // remove 'cur' from the list.
                    if (prev == NULL)
                        // if we are removing the 1st node.
                        head = head->next;
                    else
                        // good when cur is not the head.
                        prev->next = cur->next;
    
                    delete cur;
                    return;
                }
            }
           /*
            Node *temp = head;
            if(temp->element == index.curr->element)
            {
                if(index.prev == NULL)
                {
                    index.prev = head->next;
                    index.curr = index.prev->next;
                    delete head;
                    head = index.prev;
                    return;
                }
                if(index.curr->next == NULL)
                {
                    index.prev->next = NULL;
                    delete index.curr;
                    return;
                }
             temp = index.curr;
             index.prev->next = index.curr->next;
             index.curr = index.curr->next;
             delete temp;
             return;
            
            }
    */
           
    
    
    
            
            /*   
                Node *temp, *pNode;
                temp = head;
                pNode = NULL;
                if(temp->element == index.curr->element)
                {
                    if(pNode == NULL)
                    {
                        pNode= head->next;
                        delete head;
                        head = pNode;
                        return;
                    }
                    else if (pNode !=NULL)
                    {
                        pNode->next = temp->next;
                        delete temp;
                        return;
                    }
    
                }
                else if (temp->element != index.curr->element)
                {
                    pNode = temp;
                    temp = temp->next;
                }
    
    */
    
            
    
            
        }
        
        // How to declare a node pointer of a structure declared
        // within another template class
        // ‘typename’ syntax needed b/c A* B can be interpreted as
        // multiply instead of a pointer declaration w/ templates
        typename SLList<Type>::Node* location;
    
    
        virtual ~SLList(void)
        {
    
            clear();
        }
    };
    
    
    
    
    template <typename Type>
    class SLLIter
    {
    
    public:
    friend class SLList<Type>;
    typename SLList<Type>::Node *curr;
    //typename SLList<Type>::Node *prev;
        // SLLIter Public Interface
    SLList<Type> * list;
    
        // SLLIter : constructor.
    SLLIter (SLList<Type> &listToIterate){ list = &listToIterate;}
        // ~SLLIter : destructor.
    virtual ~SLLIter (){}
        // begin : move the iterator to the front of the list
    void begin (){curr = list->head;}
        // end : only return true when iterator is beyond the last 
        //       item
        bool end ()
        {
        if(curr == NULL)
            return true;
        else
            return false;
        }
        // operator++ : prefix (only) ++ operator
        SLLIter<Type> &operator++ ()
        {
            
            //if(curr)
            //{   
               curr = curr->next;
               return *this;
        
            //}
        }
        // current : extract the element at the current location.
        Type &current () {return curr->element;}
        
    };

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    An = operator is used as such:

    result = rhs;

    when passed into an operator overload function, rhs is the passed in paramter, result is *this. A return value that you might want to use is bool to return success or failure.

    Here's an example (assuming the function is inline):

    Code:
    bool operator= (const SLList<Type> &rhs)
    {
    clear()   //like the constructor, get rid of all current nodes
    if (rhs.isEmpty()) return false;
    rhs.start();   //start at first node
    do
    {
    insert(rhs.getData());
    } while (rhs.next());
    }
    return true;
    }
    However, technically the above code won't work because the passed in parameter is passed as const, which you can change. When I do this, I have an iterator to loop through the rhs list so it can stay const.

    [edit]
    I whole-heartedly decided not to read all that code. So you should just replace function names or use iterators if you have one.
    [/edit]
    Last edited by neandrake; 12-07-2004 at 04:36 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
            if(this == &list)
               return *this;
            else
            {
            *this = list;
            return *this;
            }
    You are defining the assignment operator in terms of itself. Do as Thantos suggested and copy the internals of the List. In your case, you must copy the linked list, so set this->head equal to a copy of list.head, then set this->head->next equal to a copy of list.head->next, and so on. You would probably want to use a loop of some sort to traverse the list and copy its data into the this object.

  7. #7
    Codin huh?
    Join Date
    Dec 2004
    Posts
    6
    Thanx for all the help i got it squared away had to re-write the whole thing...
    {thats why you don't code when half-asleep} :P


    wow i didn't realize the code was so lengthy

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    A return value that you might want to use is bool to return success or failure.
    operator= should never fail. Only exception to this is if you had dynamically allocated memory then if allocating fails then you should throw an exception. The operator= should return the same type of as the object (basically return *this).

    If you don't then you wouldn't be able to use = like you could with built in types (ie)
    Code:
    MyClass x = y = v = z = w;

  9. #9
    Codin huh?
    Join Date
    Dec 2004
    Posts
    6
    This is the new and improved operator= overload i came up with. works great. thanx again for the help..



    Code:
    SLList<Type> &operator= (const SLList<Type> &list)
        {
            if (this == &list)
                return *this;
            
            
            this->clear();
    
            Node * temp = list.head;
            SLLIter<Type> ait (*this);
            ait.begin();
            while(temp)
            {
                
                insert(ait,temp->element);
                 ++ait;
                temp = temp->next;
            }
            return *this;
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. operator= problems
    By tallguy in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2007, 11:08 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM