C++ ';对';动态结构?

C++ ';对';动态结构?,c++,function,struct,parameters,dynamic-arrays,C++,Function,Struct,Parameters,Dynamic Arrays,现在,我正在尝试使用namePlayers函数将几个名称动态分配到一个结构数组中。但是,当我尝试编译时,会出现以下错误: | 39 |错误:调用“namePlayers”时没有匹配函数 #include <iostream> #include <cstdlib> #include <cmath> using namespace std; struct Game{ string name; int position; }; void nam

现在,我正在尝试使用namePlayers函数将几个名称动态分配到一个结构数组中。但是,当我尝试编译时,会出现以下错误:

| 39 |错误:调用“namePlayers”时没有匹配函数

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

struct Game{
    string name;
    int position;
};

void namePlayers(Game *player[], int &numberOfPlayers);
/*void play(int &size, int &player1, int &player2, int cardPile[], int board[]);
void displayRules();
int takeTurn(int &size, int &player, int cardPile[], int board[], int &opposingPlayer);
int shuffleDeck(int &size, int cardPile[]);
int switchPlaces(int &player, int &opposingPlayer);
int obstacles(int &player, int board[]);
void showState(int &player1, int &player2);
int reshuffle(int &size, int cardPile[]);
void youWin(int &player1, int &player2);*/

int main()
{
    int size = 0;
    int board[] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
    int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
    int numberOfPlayers = 0;
    int player1 = 0;
    int player2 = 0;

    Game *player = new Game[numberOfPlayers];

    namePlayers(player, numberOfPlayers);

    for(int i = 0; i < numberOfPlayers; i++){
        cout << player[i].name << endl;
    }

    //play(size, player1, player2, cardPile, board);

    return 0;
}

void namePlayers(Game *player[], int &numberOfPlayers){
    cout << "How many players are playing(up to 6)?" << endl;
    cin >> numberOfPlayers;

     for(int i = 0; i < numberOfPlayers; i++){
        cout << "Enter your name:";
        cin >> (*player[i]).name;
    }

    for(int i = 0; i < numberOfPlayers; i++){
        cout << (*player[i]).name << endl;
    }

}


/*//This is the function that plays the entire game
void play(int &size, int &player1, int &player2, int cardPile[], int board[]){
    displayRules();
    shuffleDeck(size, cardPile);
    while(player1 < 25 && player2 < 25){
            cout << "\nPlayer 1's turn!" << endl;
            takeTurn(size, player1, cardPile, board, player2);
            size++;
            reshuffle(size, cardPile);
            showState(player1, player2);
                if(player1 >= 25)
                    break;
                else
                    cout << "\nPlayer 2's turn!" << endl,
                    takeTurn(size, player2, cardPile, board, player1),
                    size++,
                    reshuffle(size, cardPile);
                    showState(player1, player2);
    }

    youWin(player1, player2);

}


//This function displays the rules of the game
void displayRules(){
    cout << "\nWelcome to GoHome! The main objective of this game is to reach Home"
            " first." << endl;
    cout << "The basic rules of the game are as follows:" << endl;
    cout << "\n-To begin the player with the shortest name goes first." << endl;
    cout << "-Each player picks a card that has a number on it and the player"
            " must moves forward that many number of spaces." << endl;
    cout << "-If a card says 'Lose A Turn', the player does nothing and the"
            "turn moves to the next player." << endl;
    cout << "-If a card says 'Switch Places', that player is allowed to switch"
            " places with any player on the board." << endl;
    cout << "-If a player lands on an obstacle, that player must move back that"
            " many number of spaces." << endl;
    cout << "-If a player lands another obstacle while moving backwards, then it"
            " does not have to move backwards again.\n"<<endl;
}


//This function does a single turn for each player
int takeTurn(int &size, int &player, int cardPile[], int board[],int &opposingPlayer){
    if(cardPile[size] == 0)
        cout << "You drew a Lose a turn card! You lose a turn!" << endl;

    else if(cardPile[size] == 5)
        cout << "You drew a Switch Places card! You must switch places with the"
                " other player!" << endl,
        switchPlaces(player, opposingPlayer);

    else
    cout << "You drew a " << cardPile[size] << "!";
        switch(cardPile[size]){
        case 1:
            cout << " Move forward " << cardPile[size] << " space on the board!" << endl;
            player += cardPile[size];
            obstacles(player, board);
            break;
        case 2:
            cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
            player += cardPile[size];
            obstacles(player, board);
            break;
        case 3:
            cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
            player += cardPile[size];
            obstacles(player, board);
            break;
        case 4:
            cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
            player += cardPile[size];
            obstacles(player, board);
            break;
        }
}


//This function shuffles the deck of cards
int shuffleDeck(int &size, int cardPile[]){
    srand(time(0));
    for(int i = 0; i < 10; i++){
            int size = rand() % 10;
            int temp = cardPile[i];
            cardPile[i] = cardPile[size];
            cardPile[size] = temp;
    }

}


//This function forces player 1 and player 2 to switch places
int switchPlaces(int &player, int &opposingPlayer){
    int temp = player;
    player = opposingPlayer;
    opposingPlayer = temp;
}


//This is the function that tells a player when they have ran into an
//obstacle and moves them back the appropriate number of spaces
int obstacles(int &player, int board[]){
    if(player == 1)
        player -= board[1],
        cout << "You ran into an obstacle! Move back 1 space!" << endl;
    else if(player == 4)
        player -= board[4],
        cout << "You ran into an obstacle! Move back 1 space!" << endl;
    else if(player == 8)
        player -= board[8],
        cout << "You ran into an obstacle! Move back 2 spaces!" << endl;
    else if(player == 12)
        player -= board[12],
        cout << "You ran into an obstacle! Move back 3 spaces!" << endl;
    else if(player == 16)
        player -= board[16],
        cout << "You ran into an obstacle! Move back 2 spaces!" << endl;
    else if(player == 20)
        player -= board[20],
        cout << "You ran into an obstacle! Move back 1 space!" << endl;

return player;

}


//This function shows what spot on the board both players are on
void showState(int &player1, int &player2){

    if(player1 == 0)
        cout << "\nPlayer 1 is at Start!" <<endl;
    else
        cout << "\nPlayer 1 is on spot " << player1 << " of the board." <<endl;


    if(player2 == 0)
        cout << "Player 2 is at Start!\n" <<endl;
    else
        cout << "Player 2 is on spot " << player2 << " of the board.\n" <<endl;

}


//This function looks to see if the pile of cards has run out
//and call for the shuffle function to reshuffle the deck
int reshuffle(int &size, int cardPile[]){
    if(size == 10)
        shuffleDeck(size, cardPile),
        size = 0;
    else
        ;
}


//This function displays a message saying who won the game
void youWin(int &player1, int &player2){
        if(player1 >= 25)
            cout << "\nWinner! Player 1 reached Home first!\n" << endl;
        else
            cout << "\nWinner! Player 2 reached Home first!\n" << endl;

}*/
#包括
#包括
#包括
使用名称空间std;
结构游戏{
字符串名;
内部位置;
};
无效姓名玩家(游戏*玩家[],int和numberOfPlayers);
/*无效播放(int&size、int&player1、int&player2、int-cardPile[],int-board[]);
无效显示规则();
内接接转身(内接和大小、内接和球员、内接牌堆[]、内接板[]、内接和对手球员);
int-shuffleDeck(int&size,int-cardPile[]);
内部交换位置(内部和玩家、内部和对手玩家);
智力障碍(智力和球员,智力板[]);
无效显示状态(int&player1、int&player2);
内部重组(内部和大小,内部卡片堆[]);
void youWin(int&player1、int&player2)*/
int main()
{
int size=0;
int board[]={0,1,0,0,1,0,0,0,2,0,0,0,3,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0};
int cardPile[10]={1,1,2,2,3,3,4,4,0,5};
int numberOfPlayers=0;
int player1=0;
int player2=0;
游戏*玩家=新游戏[玩家数量];
姓名玩家(玩家,玩家数量);
for(int i=0;icoutnamePlayers
的第一个参数类型为
Game*[]
,但您正在传入一个
Game*
。根据用法,我假设您希望更改函数以接受
Game*
,并删除函数体本身中的额外解引用。

该程序没有意义

int numberOfPlayers = 0;
//...
Game *player = new Game[numberOfPlayers];
例如,在main中的这个代码段中,变量
numberOfPlayers
的值不确定,因为它未初始化

int numberOfPlayers;

Game *player = new Game[numberOfPlayers];
由于这个原因,程序已经有了未定义的行为

进一步,第一个函数参数

void namePlayers(Game *player[], int &numberOfPlayers);
                 ^^^^^^^^^^^^^^
具有类型
Game**
,而使用类型为
Game*
的参数调用它

namePlayers(player, numberOfPlayers);
            ^^^^^^ 
最好将第二个参数声明为type
size\t
而不是type
int
。否则,用户可以输入负数,程序将再次具有未定义的行为

我将按照以下方式声明和定义函数

Game * namePlayers( size_t &numberOfPlayers )
{
    Game *player = nullptr;
    numberOfPlayers = 0;

    cout << "How many players are playing(up to 6)?" << endl;
    cin >> numberOfPlayers;


    if ( numberOfPlayers )
    {
        player = new Game[numberOfPlayers];

        for ( size_t i = 0; i < numberOfPlayers; i++ )
        {
            cout << "Enter your name:";
            cin >> player[i].name;
        }
    }

    return player;
}

任何无法发布所有代码的特殊原因,包括#includes?
numberOfPlayers
,在分配数组时都是未初始化的。您需要首先询问玩家数量,然后创建数组,然后输入玩家姓名。如果您想将数字限制为最多六个,则根本不需要动态分配。
int numberOfPlayers = 0;
//...
Game *player = new Game[numberOfPlayers];