[Linker error] ?

Hey, i've been learning from a book called Beginning C++ Programming and i've just finished chapter 6 - the tic-tac-toe game. For some unknown reason whenever i go to compile it (using Dev C++) it comes up with the following error:

  [Linker error] undefined reference to `computerMove(std::vector<char, std::allocator<char> > const&, char)' 
  ld returned 1 exit status 


I don't understand why it's giving me this error, seeing as i've copied it straight from the book :/

I was really hoping to play this game, any help would be appreciated!

Cheers,

Jake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Tic-Tac-Toe
// Plays the game of tic-tac-toe against a human opponent

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

// Global constants
const char X = 'X';
const char O = 'O';
const char EMPTY = ' ';
const char TIE = 'T';
const char NO_ONE = 'N';

// functions
void instructions();
char askYesNo(string question);
int askNumber(string question, int high, int low = 0);
char humanPiece();
char opponent(char piece);
void displayBoard(const vector<char>& board);
char winner(const vector<char>& board);
bool isLegal(const vector<char>& board, int move);
int humanMove(const vector<char>& board, char human);
int computerMove(const vector<char>& board, char computer);
void announceWinner(char winner, char computer, char human);

// main functions
int main()
{
    int move;
    const int NUM_SQUARES = 9;
    vector<char> board(NUM_SQUARES, EMPTY);
    
    instructions();
    char human = humanPiece();
    char computer = opponent(human);
    char turn = X;
    displayBoard(board);
    
    while (winner(board) == NO_ONE)
    {
        if (turn == human)
        {
                 move = humanMove(board, human);
                 board[move] = human;
        }
        else
        {
                 move = computerMove(board, computer);
                 board[move] = computer;
        }
        displayBoard(board);
        turn = opponent(turn);
    }
    
    announceWinner(winner(board), computer, human);
    
    return 0;
}

void instructions()
{
     cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
     cout << "--where human brain it pit against silicon processor\n\n";
     
     cout << "Make your move known by entering a number, 0 - 8. The number\n";
     cout << "corresponds to the desired board position, as illustrated:\n\n";
     
     cout << "      0 | 1 | 2\n";
     cout << "      -----\n";
     cout << "      3 | 4 | 5\n";
     cout << "      -----\n";
     cout << "      6 | 7 | 8\n\n";
     
     cout << "Prepare yourself, human. The battle is about to begin.\n\n";
}

char askYesNo(string question)
{
     char response;
     do
     {
          cout << question << " (y/n): ";
          cin >> response;
     } while (response != 'y' && response != 'n');
     
     return response;
}

int askNumber(string question, int high, int low)
{
    int number;
    do
    {
        cout << question << " (" << low << " - " << high << "): ";
        cin >> number;
    } while (number > high || number < low);
    
    return number;
}

char humanPiece()
{
     char go_first = askYesNo("Do you require the first move?");
     if (go_first == 'y')
     {
                  cout << "\nThen take the first move. You will need it.\n";
                  return X;
     }
     else
     {
                  cout << "\nYour bravery will be your undoing... I will go first.\n";
                  return 0;
     }
}

char opponent(char piece)
{
     if (piece == X)
        return 0;
     else
        return X;
}

void displayBoard(const vector<char>& board)
{
     cout << "\n\t" << board[0] << " | " << board [1] << " | " << board[2];
     cout << "\n\t" << "-----";
     cout << "\n\t" << board[3] << " | " << board [4] << " | " << board[5];
     cout << "\n\t" << "-----";
     cout << "\n\t" << board[6] << " | " << board [7] << " | " << board[8];
     cout << "\n\n";
}

char winner (const vector<char>& board)
{
     // all possible winning rows
     const int WINNING_ROWS[8][3] = { {0, 1, 2},
                                      {3, 4, 5},
                                      {6, 7, 8},
                                      {0, 3, 6},
                                      {1, 4, 7},
                                      {2, 5, 8},
                                      {0, 4, 8},
                                      {2, 4, 6} };
     
     const int TOTAL_ROWS = 8;
     
     /* if any winning row has three values that are the same (not empty)'
     then we will have a winner */
     for (int row = 0; row < TOTAL_ROWS; ++row)
     {
         if ( (board[WINNING_ROWS[row][0]] != EMPTY) &&
              (board[WINNING_ROWS[row][0]] == board[WINNING_ROWS[row][1]]) &&
              (board[WINNING_ROWS[row][1]] == board[WINNING_ROWS[row][2]]) )
              {
              return board[WINNING_ROWS[row][0]];
              }
     }
     
     // since nobody has won, we check for a tie (no empty squares left)
     if (count(board.begin(), board.end(), EMPTY) == 0)
        return TIE;
        
     /* since the top two arguments delare that there isn't a winner
     and it's not a tie, the game isn't over */
     return NO_ONE;
}

inline bool isLegal(int move, const vector<char>& board)
{
       return (board[move] == EMPTY);
}
     
int humanMove(const vector<char>& board, char human)
{
    int move = askNumber("Where will you move?", (board.size() - 1));
    while (!isLegal(move, board))
    {
          cout << "\nThat square is already occupied, foolish human.\n";
          move = askNumber("Where will you move?", (board.size() - 1));
    }
    cout << "Fine...\n";
    return move;
}

int computerMove(vector<char> board, char computer)
{
    cout << "I shall take square number ";
    
    //if the computer can win on the next move, it will take that move
    for (int move = 0; move < board.size(); ++move)
    {
        if (isLegal(move, board))
        {
            board[move] = computer;
            if (winner(board) == computer)
            {
                cout << move << endl;
                return move;
            }
            //done checking this move, undo it
        }
    }
    
    // if the human can win on the next move, block that move
    char human = opponent(computer);
    for (int move = 0; move < board.size(); ++ move)
    {
        if (isLegal(move, board))
        {
           board[move] = human;
           if (winner(board) == human)
           {
              cout << move << endl;
              return move;
           }
           //done checking this move, undo it
           board[move] = EMPTY;
        }
    }
    
    
    // best moves to make, in order
    const int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
    //since no one can win on the next move, pick the best open square
    for (int i = 0; i < board.size(); ++i)
    {
        int move = BEST_MOVES[i];
        if (isLegal(move, board))
        {
           cout << move << endl;
           return move;
        }
    }
}

void announceWinner(char winner, char computer, char human)
{
     if (winner == computer)
     {
        cout << winner << "'s won!\n";
        cout << "As i predicted, human, I am triumphant once more -- proof\n";
        cout << "that computers are superior to human in all regards.\n";
     }
     
     else if (winner == human)
     {
          cout << winner << "'s won!\n";
          cout << "No, no! It cannot be! Somehow you tricked me, human.\n";
          cout << "But never again! I, the computer, so swear it!\n";
     }
     
     else
     {
         cout << "It's a tie.\n";
         cout << "You were most lucky, human, and somehow managed to tie me.\n";
         cout << "Celebrate... for this is the best you will ever achieve.\n";
     }
}
This is your declaration:
int computerMove(const vector<char>& board, char computer)
This is you definition:
int computerMove(vector<char> board, char computer)
That's why it's weird, because the computer AI needs to be able to edit the board to find out which move it will take (1. a winning move, 2. a blocking move or 3. in priory of best block) so cannot put in a const, and because it's editing the board to find the best move, it can't reference it, otherwise it will make changes to the current board.

It's exactly what the book said to do :/

cheers for the prompt reply though!

Jake.

EDIT, Problem solved
Last edited on
Topic archived. No new replies allowed.