2017-10-09 10 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

#includes를 포함하여 _all_ 코드를 게시 할 수없는 특별한 이유가 있습니까? –

+1

'numberOfPlayers'는 배열을 할당 할 때 초기화되지 않습니다. 먼저 플레이어 수를 묻고 배열을 만든 다음 플레이어의 이름을 입력해야합니다. 숫자를 최대 6 자로 제한하려면 동적 할당이 필요하지 않습니다. – molbdnilo

답변

0

namePlayers의 첫 번째 매개 변수는 Game *[]이지만, Game *으로 전달됩니다. 사용법에 따라, 나는 당신이 Game * 받아들이도록 함수를 변경하고 함수 본문 자체에서 여분의 dereferences 제거하려는 가정합니다.

0

프로그램이 이해가되지 않습니다.

예를 들어 변수 numberOfPlayers이 초기화되지 않았기 때문에이 코드 단편에서 변수 numberOfPlayers에는 불확정 값이 있습니다.

int numberOfPlayers; 

Game *player = new Game[numberOfPlayers]; 

이러한 이유로 프로그램에는 이미 정의되지 않은 동작이 있습니다.

또한 제 1 함수 파라미터

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

그것이 인자가 Game * 유형을 갖는 호출 동안 Game ** 입력 갖는다.

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

int 대신 형 size_t 갖는으로서 제 2 매개 변수를 선언하기 좋을 것이다. 그렇지 않으면 사용자는 음수를 입력 할 수 있으며 다시 프로그램에 정의되지 않은 동작이 발생합니다.

나는 선언하고 기능을 코드

size_t numberOfPlayers; 

Game *player = namePlayers(numberOfPlayers); 

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

//... 
delete [] player; 

편집과 같이 할 수있는 다음과 같은 방법

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; 
} 

그리고 주에서 정의하는 것입니다 : 당신의 코드를 변경 한 후

을 그럼에도 불구하고 귀하의 질문에이 할당은 이해가되지 않습니다.

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