View Single Post
Old Apr 29th, 2008, 11:31 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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:
c++ Syntax (Toggle Plain Text)
  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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote