'没有匹配功能来拨打'动态结构?

时间:2017-10-09 23:38:45

标签: c++ function struct parameters dynamic-arrays

现在我尝试使用namePlayers函数动态地将多个名称分配到结构数组中。但是,当我尝试编译时,我收到此错误:

|39|error: no matching function for call to '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;

}*/

编辑:这是整个代码。我评论了我目前没有工作的部分。我必须使用动态分配,因为它是分配的一部分。

2 个答案:

答案 0 :(得分:0)

Game *[]的第一个参数属于Game *类型,但您正在传递Game *。根据用法,我假设您要更改函数以接受{{1}}并删除函数体本身的额外解引用。

答案 1 :(得分:0)

该计划没有意义。

例如,在main的代码片段中,变量numberOfPlayers具有不确定的值,因为它未初始化。

int numberOfPlayers;

Game *player = new Game[numberOfPlayers];

由于这个原因,程序已经有了未定义的行为。

进一步的第一个函数参数

void namePlayers(Game *player[], int &numberOfPlayers);
                 ^^^^^^^^^^^^^^

具有类型Game **,而使用类型为Game *的参数调用它。

namePlayers(player, numberOfPlayers);
            ^^^^^^ 

最好将第二个参数声明为类型size_t而不是类型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;
}

主要代码看起来像

size_t numberOfPlayers;

Game *player = namePlayers( numberOfPlayers );

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

//...
delete [] player;

修改

在您更改了问题中的代码之后,这个分配没有意义。

int numberOfPlayers = 0;
//...
Game *player = new Game[numberOfPlayers];