Integer value getting lost somewhere

Oct 6, 2011 at 2:12am
Hi all,

I am trying to write a program which creates a list of player object and then prints the id's of them (should be 0-10) but my output is:

id = 197320
id = 197336
id = 197352
id = 197368
id = 197384
id = 197400
id = 197416
id = 197432
id = 197448
id = 197464

here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//-----------------------------------------------------------------
// Player class
//-----------------------------------------------------------------

class Player{
        int id;
public:
        Player(int num) ;
        void Print() {
          printf("id = %d \n", id);
                     }

};

class PlayerWrapper {
private:
  int id;
  Player *_player;
public:
  PlayerWrapper(int n)
  {
    id = n;
    _player = new Player(id);
    _player->Print();
  }
};

//-----------------------------------------------------------------
// Constructor for Player
//-----------------------------------------------------------------

Player::Player(int num) {
  id = num;
}



void PlayerPrint(int arg) {

// Complete this function!!
 PlayerWrapper *printIt;
  printIt= new PlayerWrapper(arg);
}



void ListPrint(List *p) {


  p->Mapcar((VoidFunctionPtr) PlayerPrint);
}


//----------------------------------------------------------------------
// ThreadTest
// This is called by the main program.
//
//----------------------------------------------------------------------

List *playerList;
Player *newPlayer;

void
ThreadTest()
{
    playerList = new List;

    for (int i = 0; i < 10; i++) {
        newPlayer = new Player(i);
        playerList->Append((void *) newPlayer);
    }

    ListPrint(playerList);
}


Thanks in advance for any ideas or help!
Oct 6, 2011 at 2:31am
I think you are printing out the address but the value. Noticed how id increased by 16 every step..
Last edited on Oct 6, 2011 at 2:31am
Oct 6, 2011 at 10:01am
That seems right, any ideas how to fix it? Google proves unhelpful and I can't figure out what I'm doing wrong when comparing it to older working programs.
Topic archived. No new replies allowed.