![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 7
Rep Power: 0
![]() |
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)
That all seems satisfactory with me, that class. Moving onwards we come to General.cpp: c++ Syntax (Toggle Plain Text)
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. |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 610
Rep Power: 3
![]() |
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)
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! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: UK
Posts: 205
Rep Power: 3
![]() |
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)
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |