Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Cannot Function C++ Linked List (http://www.programmingforums.org/showthread.php?t=15730)

mackay6 Apr 29th, 2008 10:33 PM

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:

:

  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:

:

  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.

Jessehk Apr 29th, 2008 11:31 PM

Re: Cannot Function C++ Linked List
 
Your code as it stands is very confusing to me, but that may just be because it's midnight where I am.

At the moment, your player class contains a list of players. What that means is that every player has its own list of players associated with it. Is that what you intend? It seems unlikely to me.

The implications of the first scenario mean that every time your while loop runs, the following things happen:
-- Create a new Player instance and assign it to entity (overwritting what was there previously -- memory leak!)
-- Set the fields of the Player instance.
-- Add the player instance to it's own list of player instances. This part makes little-to-no sense in my eyes.

It is for more likely that you want something like this:
:

  1. #include <iostream>
  2. #include <list>
  3. #include <string>
  4.  
  5. class Player {
  6. private:
  7.     int id_;
  8.     std::string name_;
  9. public:
  10.     explicit Player( int id, const std::string &name ) :
  11.         id_( id ),
  12.         name_( name ) {}
  13.  
  14.     int id() const { return id_; }
  15.     const std::string &name() const { return name_; }
  16. };
  17.  
  18. typedef std::list<Player> PlayerList;
  19.  
  20. int main() {
  21.     PlayerList plist;
  22.  
  23.     for ( int i = 0; i < 10; i++ )
  24.         plist.push_back( Player( i, "Bobby" ) );
  25. }


While I'd normally be very happy to explain everything in this example, I'll leave it to others since I need to go to sleep. What I will say is that you need to keep reading/learning: as far as I'm concerned, your code is gibberish. :)

Seif Apr 30th, 2008 4:22 PM

Re: Cannot Function C++ Linked List
 
If the list is to be common among all player objects then try using a static data member.


:

  1. class Player
  2. {
  3.         public:
  4.                 player(int pid, string pName);
  5.                 int id;
  6.                 string name;
  7.  
  8.                 void printList();
  9.                 static std::list<Player*> player_list;
  10. };
  11.  
  12. //    must be defined outside of class declaration
  13. std::list<Player*> Player::player_list;
  14.  
  15. Player::Player(int pid, string pName)
  16. {
  17.     id = pid;
  18.     name = pName;
  19.     player_list.push_back(this);
  20. }
  21.  
  22. int main()
  23. {
  24.     player *entity;
  25.     for (int i = 0; i < 10; i++)
  26.         entity = new player(i,"Seif");
  27.  
  28.     entity->printList();
  29.  
  30.     return 0;
  31. }


your printList() member will work fine with no modifications needed.

a brief description of whats going on:

only one copy of the static member player_list exists for all created player objects.

the static data member player_list is not considered part of the objects of type Player. They are externally linked and must therefore be defined outside the class scope.

for more info google for static members.

and on an unrelated note, try to make good use of the constructor as demonstrated above. It will make your life so much easier.


All times are GMT -5. The time now is 12:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC