operator= problems

This is a discussion on operator= problems within the C++ Programming forums, part of the General Programming Boards category; I am currently creating a circular linked class and I am having trouble with the operator= function. This is what ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    operator= problems

    I am currently creating a circular linked class and I am having trouble with the operator= function. This is what I have.
    Code:
    template <class Object>
    const CircleList<Object> & CircleList<Object>::operator=(const CircleList & rhs)
    {
      clear();
      CircleListItr<Object> ritr = rhs.returnBase();
      CircleListItr<Object> tempItr = rhs.returnBase();
      ++ritr;
       
      for ( ; ritr != tempItr; ++ritr)
          insert((*ritr).element);
           
      return *this;	
    }
    Code:
    template <typename Object>
    void CircleList<Object>::insert (const Object & x)
    {
      CircleListNode<Object>* p = theBase;
      if (p->next == 0)
       {
       	p->next = new CircleListNode<Object> ( x, p, p);
       	theBase->previous = p->next;
       }
      else
       {
       	while (p->next != theBase)
       	 {
       	  p = p->next;
       	 }
       	p->next = new CircleListNode<Object> ( x, theBase, p);
       	theBase->previous = p->next;
       }
    }
    In the main function I have List 1 which is 3 8 6 5 9 2 1 .
    So I create a List2, and do List2 = List1;

    When I run the code, I determine through debugging it only runs through the while loop four times, inserting the first four items. It does not insert the 9 or anything after it and it terminates. Anyone know where I am going wrong?

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Can't find the error yet, but you might want to check pointers for NULL rather than 0. Also, theBase isn't really defined anywhere. I assume it's a global pointer to the base...?

    Also when you're iterating through the loop to find where you should insert the next node, you can simply create a pointer to theBase->previous and then do a new from there rather than iterating.

    I also don't see code for setting the next pointer to theBase once you create a new node (after the iterations).

    There might be more to it, but those are my observations from the posted code.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by brooksbp View Post
    but you might want to check pointers for NULL rather than 0.
    If you don't know that you said do this and don't do this as being the same thing, then try to refrain from offering it as advice.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Style advice bro... I and others would prefer reading code comparing pointers to NULL rather than 0 which could be quickly overlooked as an int esp if you haven't seen the variable name declared as a pointer... you don't have to carry over the mentality that 'users are dumb' to 'programmers are dumb'...

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Anyone know where I am going wrong?
    I don't see anything wrong there. Can you post a simple main that exhibits the problem (make sure it outputs List1 before assigning it to List2). Also post the clear() function if possible.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    32
    This is my clear() function:
    Code:
    Code:
    template <typename Object>
    void CircleList<Object>::clear()
    {
      CircleListNode<Object> *p = theBase;
     
      if ( p->next == 0 || p->next ==theBase)
        return;
      
      else
      {
      	int size = sizeOfList();
      	CircleListItr<Object> itr = returnBase();
        ++itr;
        for ( int i = 0; i < size; ++i)
      	 {
      	  removeWithIterator(itr);
      	  ++itr;
      	 }
      	
      }
    }
    template <typename Object> void CircleList<Object>::removeWithIterator (const CircleListItr<Object> & itr) { CircleListNode<Object> *p = itr.current; p = p->previous; if ( p->next == 0) return; else { CircleListNode<Object> *tempNode = p->next; p->next = tempNode->next; tempNode->next->previous = p; delete tempNode; } }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I still don't see any real issues. Do you have access to a debugger? That would probably be really helpful.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
      	  removeWithIterator(itr);
      	  ++itr;
    Buggy (itr is invalid after a call to removeWithIterator), but wouldn't cause this problem.
    Callou collei we'll code the way
    Of prime numbers and pings!

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, 03:59 AM
  2. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 12:54 PM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. SLList operator= overloading problems
    By JoeS in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 11:16 AM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21