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??
#include "Check.h"
#include <iostream>
usingnamespace 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 );
}
elseif ( 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..
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:
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.