View Single Post
Old Apr 29th, 2008, 10:33 PM   #1
mackay6
Newbie
 
Join Date: Jun 2006
Posts: 7
Rep Power: 0 mackay6 is on a distinguished road
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:

c++ Syntax (Toggle Plain Text)
  1. class Player
  2. {
  3. public:
  4. int id;
  5. string name;
  6.  
  7. void printList();
  8. std::list<Player*> player_list;
  9. };

That all seems satisfactory with me, that class. Moving onwards we come to General.cpp:

c++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. Player *entity;
  4. int i = 0;
  5.  
  6. while(i != 10)
  7. {
  8. entity = new Player;
  9. entity->id = i;
  10. entity->name = "Bobby";
  11. entity->player_list.push_back(entity);
  12. i++;
  13. }
  14.  
  15. entity->printList();
  16. return 0;
  17. }
  18.  
  19. void Player::printList()
  20. {
  21. std::cout << "There are " << (int) player_list.size() << " players in the linked-list" << endl;
  22. return;
  23. }

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.
mackay6 is offline   Reply With Quote