Thread: Cannot function C++ Linked List

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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, 06: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