int variable in class suddenly changing?

May 28, 2017 at 4:26am
I'm writing a function and in the function i am trying to give numbers to each card from lowest (0) to highest (numPlayers-1) to determine who starts the game however the turn number is suddenly change when I'm checking to see what they are in main. In the function itself i printed out the turn number with no issues but my compiler tells me the turn number is 477760 but I have no idea why its doing this.

This is the function in question:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void determineLowestCard(Player& p1, Player& p2, Player& p3)
{
    std::vector<Player> playerVect;
    playerVect.push_back(p1);
    playerVect.push_back(p2);
    playerVect.push_back(p3);
    
    // < overloaded so this works fine
    std::sort(playerVect.begin(), playerVect.end());
    
    for (int i = 0; i < playerVect.size(); i++)
    {
        playerVect[i].turnNumber = i;
        std::cout << playerVect[i].playerName << " ";
        std::cout << playerVect[i].lowestCard.suit << " : " << playerVect[i].lowestCard.cardNum << std::endl;
        std::cout << playerVect[i].turnNumber << std::endl;
    }

}
May 28, 2017 at 4:55am
You change the turnNumbers of Player objects in playerVect; that vector is destroyed when the function returns, along with its contents. The Players p1, p2, and p3 you pass in are not changed by this function.
May 28, 2017 at 7:07am
I can't believe I didn't see that. Thank you soooo much for your help! I was totally stumped lol
Topic archived. No new replies allowed.