Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Apr 29th, 2008, 11:31 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
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 online now   Reply With Quote
Old Apr 30th, 2008, 4:22 PM   #3
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 213
Rep Power: 3 Seif is on a distinguished road
Re: Cannot Function C++ Linked List

If the list is to be common among all player objects then try using a static data member.


c++ Syntax (Toggle Plain Text)
  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.

Last edited by Seif; Apr 30th, 2008 at 4:32 PM.
Seif is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list problems bl00dninja C++ 6 Feb 17th, 2008 10:30 AM
error of double linked list . Pacer C 4 May 24th, 2006 11:40 PM
Linking to a linked list within a linked list Kry1010 C++ 6 Apr 6th, 2006 6:21 AM
Singly Linked List Help Firebar Java 3 May 22nd, 2005 10:56 AM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:47 PM.

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