Class object array being cleared/reset

I have the following class in a .hpp file. That file in included in all my .cpp files, two of which use this class.

1
2
3
4
5
6
7
8
9
10
11

static class Player
{
public:

    int X, Y;

    bool isAlive;

}players[10];


The first function using it assigns values, inside a for loop with the following. players[] is not being passed to the function through arguments, just have it globally available in the hpp file.

1
2
3
4
players[playerCount].X = j2;
[playerCount].Y = i;
players[playerCount].isAlive = true;
players++;


Putting break points around that point, I've determined that all the set values get cleared/reset once the function it exited. I'm not sure as to why this happens or what I can do about it. Could someone shed some light on this? If any more details are necessary or this shouldn't be here posted here please let me know which and I will provide them / seek an alternate source of help.
Generally speaking, you should only put declarations of objects in header files, not definitions. Each translation unit (cpp file) that includes the header that contains the players definition will have it's own private copy of that array.

In the header file:
1
2
3
4
5
6
7
8
9
10
class Player
{
public:

    int X, Y;

    bool isAlive;
};

extern Player players[10];  // declaration 


In a single cpp file:
Player players[10]; // definition

Hmm alright. That's interesting. Thanks!
Topic archived. No new replies allowed.