[solved] Creating header files with functions

Hey guys, so I currently have a running program "game.cpp" that runs a game of tic tack toe. I want to split the working functions into header files so that game.cpp isn't so cluttered. I have created two header files "displayBoard.h" and "gamePlay.h" but they wont compile because the functions are looking for variables that haven't been declared. First, here's the working code. The following post is what I'm trying to get it to look like. Thanks!

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

#include "displayBoard.h"
#include <iostream>
#include <limits> //This is required to catch invalid user input

class ticTacToe //A class to contain all our functions
{
    public: //Allow us to use the functions anywhere
    char board[3][3]; //Creates an 2d array for the board
    char player1Piece; //variable used in multiple functions
    char player2Piece; //varibable used in multiple

   void create_board()
    {
        //initializes a blank board
        for(int a = 0; a < 3; a++){
            std::cout << "\n";
            for (int b = 0; b < 3; b++){
                board[a][b] = '-';
                std::cout << board[a][b];
            }
        }
    }



   void print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
    {
        //prints out the updated board when called upon to do so
        for(int a = 0; a < 3; a++){
            std::cout << "\n";
            for (int b = 0; b < 3; b++){
                board[pos1][pos2] = playerPiece;
                std::cout << board[a][b];
            }
        }
    }

    int check_for_overlap(int pos1, int pos2, char playerPiece)
    {
        if (board[pos1-1][pos2-1] != '-'){
            std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
            return true;
            }
            return false;
    }

    void update_board(char playerPiece)
    {
        //updates the board based on user input
        int x, y;
        std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
        while (true){
            std::cout << "Enter row: " << std::endl;
            std::cin >> x;
            if (x < 1 || x > 3 || std::cin.fail()){
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                std::cout << "Your number is invalid. Try again. " << std::endl;
            } else {
                break;
            }
        }

        while (true)
        {
            std::cout << "Enter col: " << std::endl;
            std::cin >> y;
            if (y < 1 || y > 3 || std::cin.fail()){
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                std::cout << "Your number is invalid. Try again. " << std::endl;
            } else {
                break;
            }
        }

        if (check_for_overlap(x, y, player1Piece) == true){
            x--;y--;print_board(player2Piece, x, y);
            std::cout << "\nThe board has been re-set. Try again!" << std::endl;
        } else if (check_for_overlap(x, y, player2Piece) == true){
            x--;y--;print_board(player1Piece, x, y);
            std::cout << "\nThe board has been re-set. Try again." << std::endl;
        } else {
            x--;y--;print_board(playerPiece, x, y);
        }
    }


    int determine_winner(char playerPiece)
    {
        /*slices the board and checks if playerPiece occupies that slot.
        If the player makes a line, print that playerPiece has won
        and exit the game.*/
        if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        } else {
            return false;
        }
    }
};


void run_game()
{
    char letter_o = 'O';
    char letter_x = 'X';
    ticTacToe ttt; //creates an object "ttt" to be used later
    ttt.player1Piece = letter_x;
    ttt.player2Piece = letter_o;
    std::cout << "Welcome to tic tac toe!" << std::endl;
    std::cout << "Here is a blank board: " << std::endl;
    ttt.create_board();
    while (true){
        std::cout << "\nPlayer X, it is your turn\n" << std::endl;
        ttt.update_board(letter_x);
        if (ttt.determine_winner(letter_x) == true){
            break;
        }
        std::cout << "\nPlayer O, it is your turn\n" << std::endl;
        ttt.update_board(letter_o);
        if (ttt.determine_winner(letter_o) == true){
            break;
        }
    }
}

int main() //main kicks things off by calling "run_game()"
{
    run_game();
}

  



Last edited on
game.cpp

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

#include "gamePlay.h"
#include "displayBoard.h"
#include <iostream>
#include <limits> //This is required to catch invalid user input

class ticTacToe //A class to contain all our functions
{
    public: //Allow us to use the functions anywhere
    char board[3][3]; //Creates an 2d array for the board
    char player1Piece; //variable used in multiple functions
    char player2Piece; //varibable used in multiple
    
};



void run_game()
{
    char letter_o = 'O';
    char letter_x = 'X';
    ticTacToe ttt; //creates an object "ttt" to be used later
    ttt.player1Piece = letter_x;
    ttt.player2Piece = letter_o;
    std::cout << "Welcome to tic tac toe!" << std::endl;
    std::cout << "Here is a blank board: " << std::endl;
    ttt.create_board();
    while (true){
        std::cout << "\nPlayer X, it is your turn\n" << std::endl;
        ttt.update_board(letter_x);
        if (ttt.determine_winner(letter_x) == true){
            break;
        }
        std::cout << "\nPlayer O, it is your turn\n" << std::endl;
        ttt.update_board(letter_o);
        if (ttt.determine_winner(letter_o) == true){
            break;
        }
    }
}

int main() //main kicks things off by calling "run_game()"
{
    run_game();
}



displayBoard.h

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

#ifndef DISPLAYBOARD_H_INCLUDED
#define DISPLAYBOARD_H_INCLUDED

void create_board()
    {
        //initializes a blank board
        for(int a = 0; a < 3; a++){
            std::cout << "\n";
            for (int b = 0; b < 3; b++){
                board[a][b] = '-';
                std::cout << board[a][b];
            }
        }
    }

  void print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
    {
        //prints out the updated board when called upon to do so
        for(int a = 0; a < 3; a++){
            std::cout << "\n";
            for (int b = 0; b < 3; b++){
                board[pos1][pos2] = playerPiece;
                std::cout << board[a][b];
            }
        }
    }
    
    void update_board(char playerPiece)
    {
        //updates the board based on user input
        int x, y;
        std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
        while (true){
            std::cout << "Enter row: " << std::endl;
            std::cin >> x;
            if (x < 1 || x > 3 || std::cin.fail()){
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                std::cout << "Your number is invalid. Try again. " << std::endl;
            } else {
                break;
            }
        }



#endif // DISPLAYBOARD_H_INCLUDED



gamePlay.h

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

#ifndef GAMEPLAY_H_INCLUDED
#define GAMEPLAY_H_INCLUDED

int check_for_overlap(int pos1, int pos2, char playerPiece)
    {
        if (board[pos1-1][pos2-1] != '-'){
            std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
            return true;
            }
            return false;
    }

    

        while (true)
        {
            std::cout << "Enter col: " << std::endl;
            std::cin >> y;
            if (y < 1 || y > 3 || std::cin.fail()){
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
                std::cout << "Your number is invalid. Try again. " << std::endl;
            } else {
                break;
            }
        }

        if (check_for_overlap(x, y, player1Piece) == true){
            x--;y--;print_board(player2Piece, x, y);
            std::cout << "\nThe board has been re-set. Try again!" << std::endl;
        } else if (check_for_overlap(x, y, player2Piece) == true){
            x--;y--;print_board(player1Piece, x, y);
            std::cout << "\nThe board has been re-set. Try again." << std::endl;
        } else {
            x--;y--;print_board(playerPiece, x, y);
        }
    }

int determine_winner(char playerPiece)
    {
        /*slices the board and checks if playerPiece occupies that slot.
        If the player makes a line, print that playerPiece has won
        and exit the game.*/
        if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        }
        else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
            std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
            return true;
        } else {
            return false;
        }
    }




#endif // GAMEPLAY_H_INCLUDED

the main problem is that all the functions you moved into separate files were all member functions of your ticTacToe class. When you cut them out and put them in separate files, this ceased to be the case.

Generally, anything but the most trivial of classes are split into two files, a header file for the declarations, and a code file for the implementation.

*i'll work on this a bit and post an example of how this should be done*
tictac.h
1
2
3
4
5
6
7
8
9
10
11
12
13
class ticTacToe //A class to contain all our functions
{
    public: //Allow us to use the functions anywhere
    char board[3][3]; //Creates an 2d array for the board
    char player1Piece; //variable used in multiple functions
    char player2Piece; //varibable used in multiple

    void create_board();
    void print_board(char playerPiece, int pos1 = 0, int pos2 = 0);
    int check_for_overlap(int pos1, int pos2, char playerPiece);
    void update_board(char playerPiece);
    int determine_winner(char playerPiece);
};


tictac.cpp
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
#include "tictac.h"


void ticTacToe::create_board()
{
    //initializes a blank board
    for(int a = 0; a < 3; a++){
        std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[a][b] = '-';
            std::cout << board[a][b];
        }
    }
}


void ticTacToe::print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
{
    //prints out the updated board when called upon to do so
    for(int a = 0; a < 3; a++){
        std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[pos1][pos2] = playerPiece;
            std::cout << board[a][b];
        }
    }
}


int ticTacToe::check_for_overlap(int pos1, int pos2, char playerPiece)
{
    if (board[pos1-1][pos2-1] != '-'){
        std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
        return true;
        }
        return false;
}


void ticTacToe::update_board(char playerPiece)
{
    //updates the board based on user input
    int x, y;
    std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
    while (true){
        std::cout << "Enter row: " << std::endl;
        std::cin >> x;
        if (x < 1 || x > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    while (true)
    {
        std::cout << "Enter col: " << std::endl;
        std::cin >> y;
        if (y < 1 || y > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    if (check_for_overlap(x, y, player1Piece) == true){
        x--;y--;print_board(player2Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again!" << std::endl;
    } else if (check_for_overlap(x, y, player2Piece) == true){
        x--;y--;print_board(player1Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again." << std::endl;
    } else {
        x--;y--;print_board(playerPiece, x, y);
    }
}


int ticTacToe::determine_winner(char playerPiece)
{
    /*slices the board and checks if playerPiece occupies that slot.
    If the player makes a line, print that playerPiece has won
    and exit the game.*/
    if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    } else {
        return false;
    }
}



Then in main, make sure you include tictac.h.

You could also break up tictac.cpp into mulitple files if you wanted, so long as they all #include "tictac.h", but doing so would probably annoy some other programmer reading it, as they would probaly expect the full implementation to be in the tictac.cpp file. Unless the class is really massive and complex, try to keep 'em to one header/code file pair.


*keep in mind, I did not go through the code to make sure all the standard headers that are needed were included in these files, so you will have to do that yourself.

*edit* I forgot to put ; at end of prototypes in .h file
Last edited on
Ah ok, I have done this before but was mixed up on how to do it. Your response was excellent and very understandable. Thank you sir!
Any time. btw, we've just been discussing something similar in another thread and this: http://www.cplusplus.com/articles/Gw6AC542/ article was brought up. It's a good review. Keep in mind, declarations go in header files, definitions in code files. so long as that rule is followed, you can include the headers in as many files as you want and the linker should be able to handle it. If you put a definition in a header file, and that file gets included in multiple .cpp files, linker problems are bound to happen.
Hey guys, I still cant get main.cpp to recognize the functions. I get a compile error whenever an object is trying to be created in main.cpp. Here's my updated code:

main.cpp

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
#include "ticTacToe.h"
#include <iostream>
#include <limits> //This is required to catch invalid user input

void run_game()
{
    char letter_o = 'O';
    char letter_x = 'X';
    ticTacToe ttt; //creates an object "ttt" to be used later
    ttt.player1Piece = letter_x;
    ttt.player2Piece = letter_o;
    std::cout << "Welcome to tic tac toe!" << std::endl;
    std::cout << "Here is a blank board: " << std::endl;
    ttt.create_board();
    while (true){
        std::cout << "\nPlayer X, it is your turn\n" << std::endl;
        ttt.update_board(letter_x);
        if (ttt.determine_winner(letter_x) == true){
            break;
        }
        std::cout << "\nPlayer O, it is your turn\n" << std::endl;
        ttt.update_board(letter_o);
        if (ttt.determine_winner(letter_o) == true){
            break;
        }
    }
}

int main() //main kicks things off by calling "run_game()"
{
    run_game();
}


ticTacToe.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef TICTACTOE_H
#define TICTACTOE_H


class ticTacToe
{
    public: //Allow us to use the functions anywhere
    char board[3][3]; //Creates an 2d array for the board
    char player1Piece; //variable used in multiple functions
    char player2Piece; //varibable used in multiple

    void create_board();
    void print_board(char playerPiece, int pos1 = 0, int pos2 = 0);
    int check_for_overlap(int pos1, int pos2, char playerPiece);
    void update_board(char playerPiece);
    int determine_winner(char playerPiece);
};

#endif // TICTACTOE_H


ticTacToe.cpp

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
#include "ticTacToe.h"
#include <iostream>
#include <limits>

ticTacToe::ticTacToe()
{
     void ticTacToe::create_board()
{
    //initializes a blank board
    for(int a = 0; a < 3; a++){
        std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[a][b] = '-';
            std::cout << board[a][b];
        }
    }
}


void ticTacToe::print_board(char playerPiece, int pos1 = 0, int pos2 = 0)
{
    //prints out the updated board when called upon to do so
    for(int a = 0; a < 3; a++){
        std::cout << "\n";
        for (int b = 0; b < 3; b++){
            board[pos1][pos2] = playerPiece;
            std::cout << board[a][b];
        }
    }
}


int ticTacToe::check_for_overlap(int pos1, int pos2, char playerPiece)
{
    if (board[pos1-1][pos2-1] != '-'){
        std::cout << "\nOVERLAP DETECTED!!!!!!" << std::endl;
        return true;
        }
        return false;
}


void ticTacToe::update_board(char playerPiece)
{
    //updates the board based on user input
    int x, y;
    std::cout << "Enter a position to place the " << playerPiece << " on the board" << std::endl;
    while (true){
        std::cout << "Enter row: " << std::endl;
        std::cin >> x;
        if (x < 1 || x > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    while (true)
    {
        std::cout << "Enter col: " << std::endl;
        std::cin >> y;
        if (y < 1 || y > 3 || std::cin.fail()){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
            std::cout << "Your number is invalid. Try again. " << std::endl;
        } else {
            break;
        }
    }

    if (check_for_overlap(x, y, player1Piece) == true){
        x--;y--;print_board(player2Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again!" << std::endl;
    } else if (check_for_overlap(x, y, player2Piece) == true){
        x--;y--;print_board(player1Piece, x, y);
        std::cout << "\nThe board has been re-set. Try again." << std::endl;
    } else {
        x--;y--;print_board(playerPiece, x, y);
    }
}


int ticTacToe::determine_winner(char playerPiece)
{
    /*slices the board and checks if playerPiece occupies that slot.
    If the player makes a line, print that playerPiece has won
    and exit the game.*/
    if (board[0][0] == playerPiece && board[0][1] == playerPiece && board[0][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[1][0] == playerPiece && board[1][1] == playerPiece && board[1][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[2][0] == playerPiece && board[2][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][0] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][1] == playerPiece && board[1][1] == playerPiece && board[2][1] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][2] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][0] == playerPiece && board[1][1] == playerPiece && board[2][2] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    }
    else if(board[0][2] == playerPiece && board[1][1] == playerPiece && board[2][0] == playerPiece){
        std::cout << "\nPlayer " << playerPiece << " wins!" << std::endl;
        return true;
    } else {
        return false;
    }
}

}

}
Last edited on
I get a compile error

What error?
unidentified reference to 'ticTacToe: :create_board()'

and then it says that for all of my other functions.
Last edited on
Are you linking together object files from main and tictactoe to create your executable file?

If you are using an IDE (Integrated Development Environment), are you including main.cpp and tictactoe.cpp in your project?

If neither of those questions make sense to you, then:

What IDE (Integrated Development Environment) are you using? (Code Blocks, Microsoft Visual Studio, etc.)

What compiler are you using?
Last edited on
I'm using code Blocks.
I got it, thanks for all of the help guys!
Topic archived. No new replies allowed.