0

I'm new to C++ and programming in general and am trying to learn by creating a sort of game as I go along. I can't find any information on how to achieve what I need to do. I have created the following code, which I believe creates new objects of class Player off the heap, and creates pointers to these objects in an array.

int playerObjects(int n, int gameMode)
{
    Player* playerArray = new Player[n];
    for (int i = 0; i < n; i++)
    {
        playerArray[i].balance = 50;
        playerArray[i].score = 0;
        playerArray[i].playerNum = (i+1);
        int m = (i+1);
        playerArray[i].playerName = playerArray[i].playerN(m);
        string playerNam = playerArray[i].playerName;
        playerArray[i].playerAge = playerArray[i].playerA(playerNam);
        playerArray[i].teamNum = 0;
    }
}

where n is the number of players (from 1-4). The class Player I have created myself: What I now want to do is return to the calling function, main(), and still be able to access and modify these objects. I cannot figure out how. I have attempted to create pointers to each element of the array, like so:

Player** pOne = playerArray[0];
Player** pTwo = playerArray[1];
Player** pThree = playerArray[2];
player** pFour = playerArray[3];

which I think declares pOne to be a pointer to a pointer to an object of class Player (the array element), however, this throws the error: cannot convert 'Player' to 'Player**' in initialization

doing it like this throws the same error, but in assignment rather than initialization (obviously):

Player** pOne;
pOne = playerArray[0];

How do I do it? And, once I have done it, how do I then pass this from main() to other functions that also need to have access to these? Would it be better to declare the array globally?

Thanks

Jens Mühlenhoff
  • 13,744
  • 6
  • 47
  • 101
Matthew
  • 86
  • 1
  • 4
  • 4
    Serious overuse of pointers. Use `std::vector` and spare yourself all of these headaches.. – PaulMcKenzie Sep 02 '16 at 11:26
  • Regarding what PaulMcKenzie has said, this Q&A has helped me personally to understand why pointers are more often than not unnecessary in c++. http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself – audio Sep 02 '16 at 11:36

3 Answers3

0

The function musts to return (either as the return value or as a referenced parameter) the pointer to the first element of the created array. Thus in main you can use the pointer with the subscript operator.

Or more better approach is to use standard container std::vector<Player> and return it from the function.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
0

The easiest way is probably to just return the pointer.

Player* playerObjects(int n, int gameMode)
{
    Player* playerArray = new Player[n];
    ...
    return playerArray;
}

Alternatively if you want to keep the return value as an int, you can pass a pointer to a pointer to the function. You can then create the array in the specified pointer.

int playerObjects(int n, int gameMode, Player** playerArray)
{
    *playerArray = new Player[n];
    for (int i = 0; i < n; i++)
    {
        *playerArray[i].balance = 50;
        *playerArray[i].score = 0;
    ...
    }
}

You can call this function by doing:

Player* playerArray;
playerObjects(n, gameMode, &playerArray)

And then access the items of playerArray as usual:

playerArray[0].xyz;

Don't forget that after you've allocated memory with with new[], you need to delete it with delete[] when you're finished with it.

js441
  • 1,144
  • 7
  • 16
0

playerArray[0] will return object of type Player, so typecast operation you are doing is incorrect. If you want to use this array in main() then you can return playerArray from function playerObjects().

Anurag
  • 1
  • 1