global variable to be used by different classes

How to make classes to use the global variable which resides in main.cpp?
I have a project with multiple files, for my problem now I have a main.cpp, Tiles.hpp and .cpp, and Player.hpp and .cpp..
the global variable is an instance of class Tiles
Tiles boardTiles
which is a data structure that stores colour and number in a linked list form.
I want to make Player class access to the boardTiles, I want to take value of the nodes from the boardTiles and copy into each player's rack, which is a composition of Tiles class too.

The method defined for copying the value to players' rack is shown below. This method is inside Player.cpp
1
2
3
4
5
6
7
8
9
10
11
void Player::playerGetTiles()
{
    for(int i=0;i<14;++i)
    {
        COLOUR c = boardTiles.getTailColour();
        int n = boardTiles.getTailNumber();
        rack.push_back(c,n);
        boardTiles.pop_back();
    }

}
Last edited on
In cpp files that need to know about the global variable that is created in some other cpp file, define the global variable with extern on the front

extern Tiles boardTiles;
Topic archived. No new replies allowed.