簡體   English   中英

Xcode中的C ++鏈接器錯誤

[英]C++ Linker Error in Xcode

當我去運行以下代碼時,Xcode表示存在鏈接器錯誤。 問題是什么?

錯誤:

架構x86_64的未定義符號:“ player1IsWinnerIf()”,引用自:main.o中的_main“ player2IsWinnerIf()”,引用自:main.o中的_main

ld:找不到體系結構x86_64的符號clang:錯誤:鏈接器命令失敗,退出代碼為1(使用-v查看調用)

碼:

#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])
char player1IsWinnerIf();

char Player1IsWinnerIf() {...}

你能發現區別嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM