2013-10-30 102 views
0

當我運行以下代碼時,Xcode表示存在鏈接器錯誤。問題是什麼?Xcode中的C++鏈接器錯誤

錯誤:

Undefined symbols for architecture x86_64: "player1IsWinnerIf()", referenced from: _main in main.o "player2IsWinnerIf()", referenced from: _main in main.o

ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

代碼:

#include <iostream> 
using namespace std; 

const int numRow = 3; 
const int numColumn = 3; 
const char blank = ' '; 

class position 
{ 
public: 
position(); 
//Postcondition 
void positionPlacement()const; 

private: 
char board[3][3]; 
}; 

void InitializeBoard(char board[][numColumn], int R, int C); 
void DisplayBoard(char board[][numColumn], int R); 
void positionPlacementPlayer1(); 
void positionPlacementPlayer2(); 
char player1IsWinnerIf(); 
char player2IsWinnerIf(); 
int positionOption = 0; 

int main() 
{ 
char board[numRow][numColumn]; 
int Row = 3; 
int Column = 3; 
char response = 'Y'; 

while(response == 'Y' || response == 'y') 
{ 
    InitializeBoard(board, Row, Column); 
    DisplayBoard(board, Row); 


    do 
    { 
     //player 1 turn 
     positionPlacementPlayer1(); 
     DisplayBoard(board, Row); 
     player1IsWinnerIf(); 

     //player 2 turn 
     positionPlacementPlayer2(); 
     DisplayBoard(board, Row); 
     player2IsWinnerIf(); 
    } 


    while (player1IsWinnerIf() != '1' || player2IsWinnerIf() != '1'); 

    if(player1IsWinnerIf() == '1') 
    { 
     DisplayBoard(board, Row); 
     cout<<"Player one is the winner!"<<endl; 
     cout<<"Would you like to play again?"<<endl; 
     cin>>response; 
    } 
    else if(player2IsWinnerIf() == '1') 
    { 
     DisplayBoard(board, Row); 
     cout<<"Player two is the winner!"<<endl; 
     cout<<"Would you like to play again?"<<endl; 
     cin>>response; 
    } 
    else 
    { 
     DisplayBoard(board, Row); 
     cout<<"Draw. Neither player won."<<endl; 
     cout<<"Would you like to play again?"<<endl; 
     cin>>response; 
    } 
} 

return 0; 
} 

void InitializeBoard(char board[][numColumn], int R, int C) 
{ 
int row, column; 
for (row = 0; row<R; row++) 
    for(column = 0; column<C; column++) 
     board[row][column] = blank; 
} 

void DisplayBoard(char board[][numColumn], int R) 
{ 
int row; 
cout<<"This is a TicTacToe Board."<<endl; 
for (row = 0; row<R; row++) 
{ 
    cout<<' '<<board[row][0]<<'|'<<board[row][1]<<'|'<<board[row][2]<<endl; 
    cout<<"-------"<<endl; 
} 
} 


char board[3][3]; 


void positionPlacementPlayer1() 
//doesn't need a variable in() because it asks for one below 
{ 
cout<<"Player 1 (X), please choose an available position (1-9). For example, if you would like 
the top middle, you would enter '2', if you wanted the bottom left, you would enter '7', 
and so on and so forth."; 
cin>>positionOption; 
switch(positionOption) 
{ 
    case '1': board[0][0] = 'X'; 
     break; 
    case '2': board[0][1] = 'X'; 
     break; 
    case '3': board[0][2] = 'X'; 
     break; 
    case '4': board[1][0] = 'X'; 
     break; 
    case '5': board[1][1] = 'X'; 
     break; 
    case '6': board[1][2] = 'X'; 
     break; 
    case '7': board[2][0] = 'X'; 
     break; 
    case '8': board[2][1] = 'X'; 
     break; 
    case '9': board[2][2] = 'X'; 
     break; 
    default: 
     cout<<"This is not a valid position."<<endl; 
} 
} 

void positionPlacementPlayer2() 
{ 
cout<<"Player 2 (O), please choose an available position (1-9). For example, if you would like 
the top middle, you would enter '2', if you wanted the bottom left, you would enter '7', and so 
on and so forth."; 
cin>>positionOption; 
switch(positionOption) 
{ 
    case '1': board[0][0] = 'O'; 
     break; 
    case '2': board[0][1] = 'O'; 
     break; 
    case '3': board[0][2] = 'O'; 
     break; 
    case '4': board[1][0] = 'O'; 
     break; 
    case '5': board[1][1] = 'O'; 
     break; 
    case '6': board[1][2] = 'O'; 
     break; 
    case '7': board[2][0] = 'O'; 
     break; 
    case '8': board[2][1] = 'O'; 
     break; 
    case '9': board[2][2] = 'O'; 
     break; 
    default: 
     cout<<"This is not a valid position."<<endl; 
} 
} 

char Player1IsWinnerIf() 
{ 
//player 1 can only be the winner if they have a full row of X, full column of X, or a full 
//diagonal of X 

if(board[0][0]=='X' && board[0][1]=='X' && board && board[0][2]=='X') 
    //row 0 
    return 1; 
else if(board[1][0]=='X' && board[1][1]=='X' && board[1][2]=='X') 
    //row 1 
    return 1; 
else if(board[2][0]=='X' && board[2][1]=='X' && board[2][2]=='X') 
    //row 2 
    return 1; 
else if(board[0][0]=='X' && board[1][0]=='X' && board[2][0]=='X') 
    //column 0 
    return 1; 
else if(board[0][1]=='X' && board[1][1]=='X' && board[2][1]=='X') 
    //column 1 
    return 1; 
else if(board[0][2]=='X' && board[1][2]=='X' && board[2][2]=='X') 
    //column 2 
    return 1; 
else if(board[0][0]=='X' && board[1][1]=='X' && board[2][2]=='X') 
    //diagonal 
    return 1; 
else if(board[0][2]=='X' && board[1][1]=='X' && board[2][0]=='X') 
    //diagonal 
    return 1; 
else 
    return -1; 
} 

char Player2IsWinnerIf() 
{ 
//player 2 can only be the winner if they have a full row of O, full column of O, or a full 
//diagonal of O 

if(board[0][0]=='O' && board[0][1]=='O' && board[0][2]=='O') 
    //row 0 
    return 1; 
else if(board[1][0]=='O' && board[1][1]=='O' && board[1][2]=='O') 
    //row 1 
    return 1; 
else if(board[2][0]=='O' && board[2][1]=='O' && board[2][2]=='O') 
    //row 2 
    return 1; 
else if(board[0][0]=='O' && board[1][0]=='O' && board[2][0]=='O') 
    //column 0 
    return 1; 
else if(board[0][1]=='O' && board[1][1]=='O' && board[2][1]=='O') 
    //column 1 
    return 1; 
else if(board[0][2]=='O' && board[1][2]=='O' && board[2][2]=='O') 
    //column 2 
    return 1; 
else if(board[0][0]=='O' && board[1][1]=='O' && board[2][2]=='O') 
    //diagonal 
    return 1; 
else if(board[0][2]=='O' && board[1][1]=='O' && board[2][0]=='O') 
    //diagonal 
    return 1; 
else 
    return -1; 
} 

//switch(toupper(CINANSWER[0]) 

回答

2
char player1IsWinnerIf(); 

char Player1IsWinnerIf() {...} 

你能發現區別嗎?

+1

哇。我是個白癡。我永遠都看不到。 – kurtdawg24