2017-10-09 69 views
-1

現在我試圖動態分配幾個名稱到一個使用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; 

}*/ 

編輯:這裏是整個代碼。我評論了我目前沒有工作的部分。我必須使用動態分配,因爲它是分配的一部分。

+0

任何特殊原因您不能發佈_all_代碼,包括#includes? –

+1

當您分配數組時,'numberOfPlayers'未初始化。您需要先詢問玩家人數,然後創建陣列,然後輸入玩家的姓名。如果您想將數字限制爲最多六個,則根本不需要動態分配。 – molbdnilo

回答

0

namePlayers的第一個參數是Game *[]類型,但是您傳遞的是Game *。根據使用情況,我假設您想要更改函數以接受Game *並刪除函數體中的額外解引用。

0

程序沒有意義。

例如在此代碼段中主變量numberOfPlayers具有不確定的值,因爲它未初始化。

int numberOfPlayers; 

Game *player = new Game[numberOfPlayers]; 

由於這個原因程序已經有未定義的行爲。

此外所述第一函數參數

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

具有類型Game **而它被稱爲與具有類型Game *的論點。

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

這將是更好的聲明的第二個參數爲具有代替類型int的類型size_t。否則,用戶可以輸入一個負數,並且該程序會再次出現未定義的行爲。

我想聲明並定義函數通過以下方式

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]; 
相關問題