checkers class! help.. (was not declared issue )

I'm try to learn how to make classes and header files and multiple source files.
Attempting to make a simple checkers game and running into a problem.
For some reason my std::string board array can used in my createBoard() and displayboard() functions but not in my initReds and initBlacks functions??

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

class Check
{
    public:
        Check();

        void start_game ();

    protected:
    private:
        std::string board [8][8];
        void create_board ();
        void displayBoard ();
        void initReds( int row, int collumn);
        void initBlacks( int row, int collumn);
        void make_move ();
};

#endif // CHECK_H 


Checker functions source file:
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
#include "Check.h"
#include <iostream>

using namespace std;

Check::Check()
{
    std::string board ;
}

void Check::start_game()
{
    cout << "Creating your board:\n\n";
    create_board();
}
void Check::create_board()
{
    for ( int i = 0; i < 8; i++ )
    {
        for ( int j = 0; j < 8; j++ )
        {
            board [i][j] = "[ ] ";
            if ( j < 3 )
            {
                initReds( i, j );
            }
            else if ( j > 4 )
            {
                initBlacks( i, j );
            }
        }
    }
    displayBoard();
}


void Check::displayBoard()      //going to be main display function for remainder
{
    for ( int i = 0; i < 8; i++ )
    {
        for ( int j = 0; j < 8; j++ )
        {
               cout << board [i][j];
        }
        cout << "\n\n";
    }
}

void initReds ( int row, int collumn)    //used only in initial board creation
{
                if ( ( ( row + collumn ) % 2 ) != 0 )
                {
                    board [row][collumn] = "[O] ";
                }
}
void initBlacks ( int row, int collumn)   //also used only in initial board creation
{
                if ( ( ( row + collumn ) % 2 ) != 0 )
                {
                    board [row][collumn] = "[X] ";
                }
}


Why is this? the board array worked perfectly as is in the createboard and displayboard functions but then I added the initreds and initblacks functions and it says "board was not declared in this scope" for both initreds and initblacks. i should probably add that both the initReds and initBlacks functions were nested if statements in place of where their function calls are now and it worked perfectly then, it was only after I turned them into their own functions to make things more readable that this started..

thanks
Last edited on
Line 49: initReds is a global function, not a member of your Check class. As a global function it can not see private members of your Check class. Change the function header to:
 
void Check::initReds ( int row, int collumn)  


Line 56: Ditto for initBlacks.
gah. didn't even notice that... thank you!

any other tips as far as my formatting goes? I'm trying to make it as concise as possible.

all pointers welcome
1) you're missing a #include for <string>. In some implementations, <string> is included indirectly by <iostream>, but that is implementation defined behavior and should not be relied upon if you want your programs to be portable.

2) I'd invert the order of your includes.
1
2
3
#include <iostream>
#include <string>
#include "Check.h" 


That way, any classes (such as string) are known before the compiler processes your check.h header.


Topic archived. No new replies allowed.