Cannot function C++ Linked List

This is a discussion on Cannot function C++ Linked List within the C++ Programming forums, part of the General Programming Boards category; Hello all, This is my first post and I'm encountering a problem with a program I'm trying to create for ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Cannot function C++ Linked List

    Hello all,

    This is my first post and I'm encountering a problem with a program I'm trying to create for a small game. What it involves is basically adding a Player onto a list using std::list.

    Now, the issue with that is no matter what I do, only one of the values register so I need help to show you what is going wrong. Here is both General.h and General.cpp

    First of all: General.h

    Code:
    class Player
    {
    	public:
    		int id;
    		string name;
    
    		void printList();
    		std::list<Player*> player_list;
    };
    That all seems satisfactory with me, that class. Moving onwards we come to General.cpp:

    Code:
    int main()
    {
    	Player *entity;
    	int i = 0;
    
    	while(i != 10)
    	{
    		entity = new Player;
    		entity->id = i;
    		entity->name = "Bobby";
    		entity->player_list.push_back(entity);
    		i++;
    	}
    
    	entity->printList();
    	return 0;
    }
    
    void Player::printList()
    {
    	std::cout << "There are " << (int) player_list.size() << " players in the linked-list" << endl;
    	return;
    }
    Okay so this seems decent to me and I've browsed many topics of linked-lists to come to this code. So my question to all of you is why is it only say that the list is a mere size of 1 when it should be right up to 11?

    No idea, hopefully someone can assist me as soon as possible! Thanks.
    Last edited by xmr-mackayx; 04-29-2008 at 09:07 PM. Reason: Added in insertion operator

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    You are missing an insertion operator (<<) in your cout statement in printList()
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Okay amended that, it was just simply a typo not in the code itself, but what I had just typed up there onto the post.

    No idea as to what the source of the problem could be?

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Because each time through the while loop you are disregarding any previous Player objects and starting over fresh with a new Player;.

    Since entity is just a pointer to a player object, when you assign it to a newly allocated player with
    Code:
    entity = new Player;
    new is returning a new pointer which is then assigned to entity, and everything you did with the old entity is lost.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Even though it has already been added to the tail end of the player list?

    If so, what is it that I'm missing for this piece of code to function correctly?

  6. #6
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    What you are doing currently is like creating a person who is holding a bucket, having him step into his bucket, and then on the next iteration of your loop creating a new person and having him step into his bucket. On the 10th iteration (not the 11th) you create a 10th person and have him step into his bucket. The other 9 people are ignored and forgotten at this point. So when you tell that 10th person to reveal how many people are in the bucket, he only sees himself.

    You are confusing the logic of the list here. You want to have a list that contains Players, but what you are doing is creating a player who contains a list.

    The class itself is mostly ok, but unless you want each player to have access to the list that it is in, you don't need to include the std::list<Player*> player_list.

    Instead, in main you would want to create a list
    Code:
    int main()
    {
      std::list<Player *> p_list;
    Then in your while loop you can create new Players and add them to this list
    Code:
      int i = 0;
      while (i != 10)
      {
        Player *entity = new Player;
        // set the player info here
        p_list.push_back(entity);
        i++;
      }
    Now you probably wouldn't want to task any individual player in the list with reporting the size of the list (how many players it contains) so you would just print the p_list.size().
    Code:
      std::cout << "There are " << p_list.size() << " players in the linked-list" << endl;
      return 0;
    }
    If you want to encapsulate handling how many players are in the list in a class, then you would probably want to create another class to manage the list of players. This new class would have a std::list<Player *> member, and in your while loop you would tell this class to add the Player you create and initialize in the while loop.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by xmr-mackayx View Post
    Okay amended that, it was just simply a typo not in the code itself, but what I had just typed up there onto the post.
    You should always post code that compiles and exhibits the problem - just copy/paste from your IDE or text editor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Replies: 5
    Last Post: 11-04-2006, 05:39 PM
  5. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM

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