Thread: Problem with std::list

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Problem with std::list

    I am trying to move some objects around in a std::list while looping through it. It works, but if isActive() returns false, the program crashes.

    I am sure it has something to do with how I move the data around in the std::list, but I am not sure how to fix it. Could someone please give me a hand?

    Code:
    std::list<Object*> myList;
    
    // Some code to add things to the list...
    
    std::list<Object*>::iterator it = myList.begin();
    std::list<Object*>::iterator tempIt;
    
    // Update all the active objects in the list
    while (it != myList.end())
    {
    	// Check if the object is active
    	if((*it)->isActive())
    	{
    		// Update the object, if update failes, move it to the front
    		// of the list.
    		if( !( (*it)->update() ) )
    		{
    			// Assing the current object to a temp itterator
    			tempIt = it;
    
    			// Move the main itterator to the next object
    			it++;
    
    			// Insert the object that failed the update to the start of the list
    			myList.splice(tempIt, myList,myList.begin());
    		}
    		else 
    			it++;
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Should your else it++ go with the big if (isActive) instead of the little if (update)? Otherwise you just spin until isActive magically becomes true.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks makes sense.

    I changed my code, but now it stops at once.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by "stops"?

    Anyway, I think you have splice backwards, if I'm reading the manual correctly. You're moving the head of the list into the spot marked by tempIt, not the other way around. Maybe you want
    Code:
    myList.splice(myList.begin(), myList, tempIt);
    instead?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Again, this was a case of me being stupid.

    if isAcitve is true, it++ will never happen. Also, thanks for correcting my splice function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM