简体   繁体   English

可以访问一个 class 中的播放器结构,但不能访问另一个

[英]Can access player struct in one class but not the other

I am filling two players' hands with random dominoes and have no issue.我正在用随机的多米诺骨牌填充两个玩家的手,没有问题。 When I send the struct to another class, I cannot access the players hands anymore.当我将结构发送到另一个 class 时,我无法再访问玩家的手了。

using namespace std;
struct player {
    int playerNum;
    vector < pair <int, int>> hand;
};
vector < pair <int, int>> availablePieces;

void Player::createHand(vector < pair <int, int>> &multiVector, player* player) {
    srand(time(NULL)); // rand seed
    int ranNumb;
    //fill players' hand
    for (int i = 0; i < 10; i++) {
        ranNumb = rand() % multiVector.size();
        player[0].hand.push_back(multiVector[ranNumb]);
        multiVector.erase(multiVector.begin() + ranNumb);
        ranNumb = rand() % multiVector.size();
        player[1].hand.push_back(multiVector[ranNumb]);
        multiVector.erase(multiVector.begin() + ranNumb);
    }
}
setAvailablePieces(multiVector);

void Player::createPlayers(vector < pair <int, int>>& multiVector) {
    struct player* domiPlayer = new struct player[2];
    domiPlayer[0].playerNum = 1;
    domiPlayer[1].playerNum = 2;
    createHand(multiVector, domiPlayer);

}
Game gameObj;
gameObj.API(player, availablePieces);

Game Header:游戏 Header:

using namespace std;
class Game
{
public:
    void API(struct player* player, vector < pair <int, int>> &availablePieces);
private:
    void playGame(player* player, vector < pair <int, int>>& availablePieces);
};

Game Class:游戏 Class:

void Game::playGame(player* player, vector < pair <int, int>>& availablePieces) {
    cout << player[0].hand[0].first << endl;
}
void Game::API(player* player, vector < pair <int, int>> &availablePieces) {
    playGame(player, availablePieces);
}

In the function playGame, I cannot access the individual players hands.在 function playGame 中,我无法访问个别玩家的手牌。 I get the error: Expression must be a pointer to a complete object type.我收到错误:表达式必须是指向完整 object 类型的指针。

My initial answer was incorrect (apologies).我最初的回答是不正确的(道歉)。 But after looking at this again I think I see your issue (in addition to the leaking of your player objects when createPlayers finishes as @TedLyngmo mentioned).但是在再次查看此内容后,我想我看到了您的问题(除了@TedLyngmo 提到的createPlayers完成时player对象的泄漏)。

When you create your players you use struct again.当你创建你的players时,你再次使用struct This isn't needed as you've already defined player as a struct.这不是必需的,因为您已经将player定义为结构。 See below, where I've made a more minimal example of your code, initializing players and storing them within the Game class:见下文,我为您的代码制作了一个更简单的示例,初始化players并将它们存储在Game class 中:

#include <stdio.h>

struct player {
    int test = 2;
};

class Game {
    public:
        Game(const int nPlayers);
        void playGame();
    private:
        player *players;
};

Game::Game(const int nPlayers) {
    players = new player[nPlayers];
}

void Game::playGame() {
    printf("%d\n", players[0].test);
}

int main() {
    const int nPlayers = 2;
    Game game(nPlayers);
    game.playGame();
}

This solves the problem of players going out of scope as well as not using struct again when initializing players .解决了players器在初始化players器时退出 scope 以及不再使用struct的问题。 I also omitted the Player class as it was not clear where that comes from in the code you posted.我还省略了Player class,因为在您发布的代码中不清楚它来自哪里。

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

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