简体   繁体   English

朋友有权访问朋友的成员,但似乎无法更新朋友的成员

[英]Friend has access to friend's member, but can't seem to update friend's member

Within the Dealer class, I declare Player as a friend class. 在Dealer类中,我将Player声明为朋友类。 Note that deck is a Dealer member, and I have the following Dealer function: 请注意,deck是经销商会员,并且我具有以下经销商功能:

deque<pair<int, string>> Dealer::deal(int numOfCards){
    deque<pair<int, string>> dealtCards;

    for (int i = 1; i <= numOfCards; i++)
    {       
        dealtCards.push_back(deck.front());
        deck.pop_front(); // once a card is dealt, delete it from the deck
    }

    return(dealtCards);
}

When I call this function directly, for example in main(), the deck is updated appropriately (front cards are deleted). 当我直接调用此函数时,例如在main()中,将相应地更新牌组(删除前面的卡)。 However, when I call this function from the Player class, the Dealer deck member is not updated. 但是,当我从Player类调用此函数时,发牌人的甲板成员不会更新。 For example, I'm expecting this function to update Dealer deck, but it's not: 例如,我期望此功能更新Dealer牌组,但不是:

Player::Player(Dealer dealer, int numOfCards){// deal numOfCards to player
    holeCards = dealer.deal(numOfCards);
}

I don't see what's the difference. 我看不出有什么区别。 Player does have access to Dealer's private member deck, as I can see the holeCards are updated correctly. 玩家确实有权访问经销商的私人会员卡套,因为我可以看到HoleCard已正确更新。 But for some reason, this constructor is just not performing the pop_front() portion of the deal function. 但是由于某种原因,该构造函数没有执行Deal函数的pop_front()部分。 What am I doing wrong? 我究竟做错了什么? Thank you! 谢谢!

Change 更改

Player::Player(Dealer dealer, int numOfCards)

to

Player::Player(Dealer &dealer, int numOfCards)

If you want to understand more check swap by reference example. 如果您想了解更多信息,请参考参考示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM