我的BattleShip游戏出现链接器错误,我找不到解决方法

时间:2018-11-07 06:57:39

标签: c++ linker-errors undefined-reference exitstatus

这是我尝试编译时遇到的错误。我必须在明天之前完成此操作,如果您能在那时之前帮助我,我将不胜感激。如果我禁用这些行,它将进行编译,但显然不会向用户显示该棋盘,也不会检查该棋盘是命中还是未命中,并且您会以任何猜测立即获胜。

[Linker error] Real Main Source Code.o:Real Main Source Code.cpp:(.text+0xc         [Linker error] Real Main Source Code.o:Real Main Source Code.cpp:(.text+0x9ab): undefined reference to `generate_ship(char (*) [10], int)'
        [Linker error] Real Main Source Code.o:Real Main Source Code.cpp:(.text+0xa94): undefined reference to `print_board(char (*) [10])'
        [Linker error] Real Main Source Code.o:Real Main Source Code.cpp:(.text+0xc8f): undefined reference to `check_board(char (*) [10], bool&)'
        [Linker error] Real Main Source Code.o:Real Main Source Code.cpp:(.text+0xce2): undefined reference to `print_board(char (*) [10])'
        collect2: ld returned 1 exit status
    C:\Users\theto\Documents\BattleShip Tutorial\Makefile.win   [Error] ["Main] Error 1 (if this is the only error: please check your library includes)

所有代码

#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

const int ROWS = 10;
const int COLS = 10;
void print_board(char board[][COLS]);
void generate_ship(char board[][COLS], int size);
void check_board(char board[][COLS], bool &victory);

int main(int argc, char *argv[])
{
    srand( static_cast<unsigned int>(time(NULL)));
    char board[ROWS][COLS], playerBoard[ROWS][COLS];
    int sizeShip, nShips;


    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            board[i][j] = '.';
            playerBoard[i][j] = '.';
        }
    }

    cout<<"\nWelcome to Battleship! This is a one player game and you will be able to select difficulty.\n\n";


    cout<<"____________________¶¶¶"<<endl;
    cout<<"_____________________¶"<<endl;
    cout<<"_____________________¶¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
    cout<<"_____________________¶¶¶___¶__¶_¶¶¶¶"<<endl;
    cout<<"_____________________¶¶¶___¶__¶_¶¶¶¶"<<endl;
    cout<<"_____________________¶¶¶___¶__¶_¶¶¶¶"<<endl;
    cout<<"_____________________¶¶¶___¶¶¶¶___¶¶"<<endl;
    cout<<"_____________________¶¶¶__¶¶¶¶¶___¶¶"<<endl;
    cout<<"_____________________¶¶¶__¶¶¶¶¶___¶"<<endl;
    cout<<"_____________________¶¶¶¶¶¶__¶¶___¶"<<endl;
    cout<<"_____________________¶_________¶¶¶¶"<<endl;
    cout<<"_____________________¶_________¶¶¶¶"<<endl;
    cout<<"_____________¶¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
    cout<<"_____________¶¶___________¶¶"<<endl;
    cout<<"______________¶____________¶"<<endl;
    cout<<"______________¶____________¶"<<endl;
    cout<<"______________¶_____________¶"<<endl;
    cout<<"_______________¶____________¶"<<endl;
    cout<<"_______________¶____________¶_¶¶"<<endl;
    cout<<"_______________¶____________¶_¶¶"<<endl;
    cout<<"_______________¶__¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
    cout<<"_____¶¶¶¶¶¶¶¶¶¶¶¶¶¶______________¶"<<endl;
    cout<<"_____¶____________¶¶_____________¶¶____¶"<<endl;
    cout<<"_____¶¶____________¶_____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;
    cout<<"______¶______¶¶¶¶¶¶¶¶¶¶¶¶¶¶______________¶"<<endl;
    cout<<"______¶¶_____¶¶___________¶______________¶¶"<<endl;
    cout<<"_______¶______¶____________¶______________¶"<<endl;
    cout<<"_______¶______¶¶___________¶_____________¶¶"<<endl;
    cout<<"_______¶_______¶___________¶_____________¶¶"<<endl;
    cout<<"______¶¶_______¶___________¶¶____________¶"<<endl;
    cout<<"______¶¶_______¶___________¶¶____________¶"<<endl;
    cout<<"______¶¶¶¶¶¶¶¶¶¶¶__________¶¶___________¶¶"<<endl;
    cout<<"______¶¶¶¶¶¶¶¶¶¶¶__________¶¶___________¶¶"<<endl;
    cout<<"___________¶_¶_¶¶________¶¶¶_____¶¶¶¶¶¶¶¶_____¶¶¶"<<endl;
    cout<<"___________¶_¶_¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶_______¶¶¶¶¶__¶¶"<<endl;
    cout<<"___________¶_¶_¶¶¶¶¶¶¶¶¶¶¶_¶¶¶¶¶¶¶_______¶¶¶¶¶__¶¶"<<endl;
    cout<<"¶¶¶¶¶¶_____¶_¶______¶¶_¶_______¶_¶¶¶¶¶¶¶¶¶___¶¶¶¶¶"<<endl;
    cout<<"¶¶¶¶¶¶_____¶_¶______¶¶_¶_______¶_¶¶¶¶¶¶¶¶¶___¶¶¶¶¶"<<endl;
    cout<<"¶¶___¶¶¶¶¶¶¶¶¶______¶¶_¶____¶¶¶¶¶¶¶________¶¶"<<endl;
    cout<<"__¶¶________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶______¶"<<endl;
    cout<<"____¶____________________________¶¶_¶____¶"<<endl;
    cout<<"_____¶_____¶¶¶_____¶¶_____¶¶¶_____¶¶¶___¶¶"<<endl;
    cout<<"_____¶_____¶¶¶_____¶¶_____¶¶¶_____¶¶¶___¶¶"<<endl;
    cout<<"______¶___¶¶_¶¶___¶¶_¶____¶_¶¶__________¶"<<endl;
    cout<<"______¶¶____¶¶_____¶¶¶_____¶¶__________¶¶"<<endl;
    cout<<"______¶¶____¶¶_____¶¶¶_____¶¶__________¶¶"<<endl;
    cout<<"_______¶¶_____________________________¶¶"<<endl;
    cout<<"________¶¶___________________________¶¶"<<endl;
    cout<<"_________¶¶________________________¶¶¶"<<endl;
    cout<<"___________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶"<<endl;

    system("PAUSE");
    system("cls");

    do
    {
        cout<<"How many enemy battleships do you want? You can't have more that 5: ";
        cin>>nShips;
        system("cls");
    }

    while (nShips < 1 || nShips > 5);

    cout<<"You may choose less ships for a quick game"<<endl<<endl<<"Recommend doing:"<<endl<<endl<< "1. Carrier 5"<<endl<<
            "2. Battleship 4"<<endl<<"3. Cruiser 3"<<endl<<"4. Submarine 3"<<endl<<"5. Destroyer 2"<<endl<<endl;

    for (int i = 1; i <= nShips; i++) 
    {
        do
        {   
            cout<<"Size of enemy battleship #"<<i<<" (at least 2, up to 5):"<<endl;

            cin>>sizeShip;
        }

        while (sizeShip < 2 || sizeShip > 5);
        //generate_ship(board, sizeShip)
    }

    int nBombs = 25, iGuess, jGuess;
    bool victory = false;
    if (nShips !=1)
        cout<<"\nHere is the game board. There are" <<nShips<<"enemy battleships hidden here.\n";
    else
        cout<<"\nHere is the gameboard. There is "<<nShips<<" enemy battleship(s) hiding here.\n";
    cout<<"Input \"i\" and \"j\" (\"i\" is the row and \"j\" is the column to decide where to hit).\n"
    <<"For example, if you wanted to fire in the top right corner, type \"1 10\" (row 1 column 1). \n"
    <<"You have 25 bombs. Make your move now.\n"; //May need to change bombs to 100

        for (int n = 1; n <= nBombs && !victory; n++) {
            //print_board(playerBoard);
            cout<<"\nBomb #"<<n << ", i and j: ";
            cin>>iGuess;
            cin>>jGuess;
            if (board[iGuess - 1][jGuess - 1] == 'S') {
                cout<<"You made a hit\n";
                board[iGuess - 1][jGuess - 1] = '*';
                playerBoard[iGuess -1 ][iGuess - 1] = '*';

        }

        else if (board[iGuess - 1][jGuess - 1] == '.'){
            cout<< "You missed.\n";
            board[iGuess - 1][jGuess - 1] = 'o';
            playerBoard[iGuess - 1][jGuess - 1] = 'o';
        }


            victory = true;
            //check_board(board, victory);
    }

    cout<<"Here is the final board:\n";
    //print_board(board);
    cout<< "A \".\" is open sea.\n"<< "An \"*\" is a hit on an enemy battleship.\n"<< "An \"S\" is a surviving ship. \n"
    << "An \"o\" is a missed hit. \n\n";
    if (victory)
        cout<<"congratulations, you sunk all the enemy ships!\n\n";
    else
        cout<<"You didn't sink all enemy ships. Try again!\n\n";
    return 0;   
    //system("PAUSE");
    //return EXIT_SUCCESS;
}

void printBoard(char board[][COLS])
{
    for (int i = 0; i < ROWS; i++){
        for (int j = 0; j < COLS; j++){
            cout<< board[i][j];
        }
        cout<<endl;
    }
    return;
}
void generateShip(char board[][COLS], int size)
{
    int iStart, jStart, ort;

    Randomize:
        do
        {
            iStart = rand()%ROWS;
            jStart = rand()%COLS;
            ort = rand()%2;
        }
        while (board[iStart][jStart] == 'S');

        if (ort == 0)
        {
            while (jStart + size >= COLS){
                jStart = rand()%COLS;
            }
            for (int j = jStart; j < jStart + size; j++) {
                if (board[iStart][j] == 'S')
                goto Randomize;

            }
            for (int j = jStart; j < jStart + size; j++){
                board[iStart][j] == 'S';
            }
        }
        if (ort == 1){
            while (iStart + size >= ROWS){
                iStart = rand()%ROWS;
            }
            for (int i = iStart; i < iStart + size; i++){
                if (board[i][jStart] == 'S')
                goto Randomize;
            }
            for (int i = iStart; i < iStart + size; i++ ){
                board[i][jStart] = 'S';
            }
        }
}

我对此很陌生,如果你们还需要其他东西,或者我违反了问类似问题的规则,但是我见过类似问题并且无法解决,那就对不起。

0 个答案:

没有答案