지금은 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;
}*/
편집 : 여기에 전체 코드입니다. 나는 현재 작업하고 있지 않은 부분을 주석 처리했다. 할당의 일부이기 때문에 동적 할당을 사용해야합니다.
#includes를 포함하여 _all_ 코드를 게시 할 수없는 특별한 이유가 있습니까? –
'numberOfPlayers'는 배열을 할당 할 때 초기화되지 않습니다. 먼저 플레이어 수를 묻고 배열을 만든 다음 플레이어의 이름을 입력해야합니다. 숫자를 최대 6 자로 제한하려면 동적 할당이 필요하지 않습니다. – molbdnilo