Help with undeclared identifier
Apr 3, 2014 at 8:57pm UTC
I have to make a connect four game. When i am trying to declare the board, which has to be a 6x7 string matrix, i get that it is undeclared. Please any help is welcome. There are also two other files, another header and cpp file that i am not including.
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 264 265 266 267 268 269 270 271 272 273 274
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
#include <string>
#include <iomanip>
using std::string;
using std::ostream;
using std::endl;
const int rows = 6;
const int cols = 7;
const int diags = 24;
class Board
{
private :
string board[rows][cols];
int checkerCount;
bool checkRow( int row );
bool checkCol( int col );
bool checkDiag( int dia );//there are 18 locations where you can make a diagonal. This number represents one of them. I have numbered them from the top left starting with 0
public :
Board();
bool win();
bool draw();
bool dropChecker( string checker, int icol );
bool gameOver();
void displayBoard( ostream& out );
void resetBoard();
};
#endif
//////NOW THIS IS A SEPARATE FILE
#include "Board.h"
#include <iomanip>
using namespace std;
using std::string;
Board::Board()
{
int checkerCount=0;
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
board[i][j]= "" ;
}
}
}
bool checkCol( int col )
{
if ((board[0][col]== board[1][col]) && (board[1][col] == board[2][col]) && (board[2][col] == board[3][col]) && !(board[0][col] == "" ))
{
return true ;
}
else if ((board[1][col]== board[2][col]) && (board[2][col] == board[3][col]) && (board[3][col] == board[4][col]) && !(board[1][col] == "" ))
{
return true ;
}
else if ((board[2][col]== board[3][col]) && (board[3][col] == board[4][col]) && (board[4][col] == board[5][col]) && !(board[2][col] == "" ))
{
return true ;
}
else
{
return false ;
}
}
bool Board::checkRow( int row)
{
if ((board[row][0]== board[row][1]) && (board[row][1] == board[row][2]) && (board[row][2] == board[row][3]) && !(board[row][0] == "" ))
{
return true ;
}
else if ((board[row][1]== board[row][2]) && (board[row][2] == board[row][3]) && (board[row][3] == board[row][4]) && !(board[row][1] == "" ))
{
return true ;
}
else if ((board[row][2]== board[row][3]) && (board[row][3] == board[row][4]) && (board[row][4] == board[row][5]) && !(board[row][2] == "" ))
{
return true ;
}
else if ((board[row][3]== board[row][4]) && (board[row][4] == board[row][5]) && (board[row][5] == board[row][6]) && !(board[row][3] == "" ))
{
return true ;
}
else
{
return false ;
}
}
bool Board::checkDiag( int dia)
{
if ((board[0][0]== board[1][1]) && (board[1][1] == board[2][2]) && (board[2][2] == board[3][3]) && !(board[0][0] == "" ))
{
return true ;
}
else if ((board[0][1]== board[1][2]) && (board[1][2] == board[2][3]) && (board[2][3] == board[3][4]) && !(board[0][1] == "" ))
{
return true ;
}
else if ((board[0][2]== board[1][3]) && (board[1][3] == board[2][4]) && (board[2][4] == board[3][5]) && !(board[0][2] == "" ))
{
return true ;
}
else if ((board[0][3]== board[1][4]) && (board[1][4] == board[2][5]) && (board[2][5] == board[3][6]) && !(board[0][3] == "" ))
{
return true ;
}
else if ((board[1][0]== board[2][1]) && (board[2][1] == board[3][2]) && (board[3][2] == board[4][3]) && !(board[1][0] == "" ))
{
return true ;
}
else if ((board[1][1]== board[2][2]) && (board[2][2] == board[3][3]) && (board[3][3] == board[4][4]) && !(board[1][1] == "" ))
{
return true ;
}
else if ((board[1][2]== board[2][3]) && (board[2][3] == board[3][4]) && (board[3][4] == board[4][5]) && !(board[1][2] == "" ))
{
return true ;
}
else if ((board[1][3]== board[2][4]) && (board[2][4] == board[3][5]) && (board[3][5] == board[4][6]) && !(board[1][3] == "" ))
{
return true ;
}
else if ((board[0][6]== board[1][5]) && (board[1][5] == board[2][4]) && (board[2][4] == board[3][3]) && !(board[0][6] == "" ))
{
return true ;
}
else if ((board[0][5]== board[1][4]) && (board[1][4] == board[2][3]) && (board[2][3] == board[3][2]) && !(board[0][5] == "" ))
{
return true ;
}
else if ((board[0][4]== board[1][3]) && (board[1][3] == board[2][2]) && (board[2][2] == board[3][1]) && !(board[0][4] == "" ))
{
return true ;
}
else if ((board[0][3]== board[1][2]) && (board[1][2] == board[2][1]) && (board[2][1] == board[3][0]) && !(board[0][3] == "" ))
{
return true ;
}
else if ((board[1][6]== board[2][5]) && (board[2][5] == board[3][4]) && (board[3][4] == board[4][3]) && !(board[1][6] == "" ))
{
return true ;
}
else if ((board[1][5]== board[2][4]) && (board[2][4] == board[3][3]) && (board[3][3] == board[4][2]) && !(board[1][5] == "" ))
{
return true ;
}
else if ((board[1][4]== board[2][3]) && (board[2][3] == board[3][2]) && (board[3][2] == board[4][1]) && !(board[1][4] == "" ))
{
return true ;
}
else if ((board[1][3]== board[2][2]) && (board[2][2] == board[3][1]) && (board[3][1] == board[4][0]) && !(board[1][3] == "" ))
{
return true ;
}
else if ((board[1][6]== board[2][5]) && (board[2][5] == board[3][4]) && (board[3][4] == board[4][3]) && !(board[1][3] == "" ))
{
return true ;
}
else if ((board[0][6]== board[1][5]) && (board[1][5] == board[2][4]) && (board[2][4] == board[3][3]) && !(board[0][6] == "" ))
{
return true ;
}
else if ((board[2][6]== board[3][5]) && (board[3][5] == board[4][4]) && (board[4][4] == board[5][3]) && !(board[2][6] == "" ))
{
return true ;
}
else if ((board[0][3]== board[1][4]) && (board[1][4] == board[2][5]) && (board[2][5] == board[3][6]) && !(board[0][3] == "" ))
{
return true ;
}
else if ((board[1][3]== board[2][4]) && (board[2][4] == board[3][5]) && (board[3][5] == board[4][6]) && !(board[1][3] == "" ))
{
return true ;
}
else if ((board[2][3]== board[3][4]) && (board[3][4] == board[4][5]) && (board[4][5] == board[5][6]) && !(board[2][3] == "" ))
{
return true ;
}
else
return false ;
}
bool Board::dropChecker( string checker, int column)
{
if (column-1>=0 && column-1<8)
{
for (int i = 5; i>=0; i--)
{
if (board[i][column-1]== "" )
{
board[i][column-1]= checker;
checkerCount++;
return true ;
}
}
}
else
return false ;
}
bool Board::gameOver()
{
if ( win() || draw() )
{
return true ;
}
return false ;
}
bool Board::draw()
{
if (checkerCount== 42 )
{
return !win();
}
else
return false ;
}
bool Board::win()
{
for (int i=0; i<5; i++)
{
if (checkRow(i))
{
return true ;
}
}
for (int j=0; j<6; j++)
{
if (checkCol(j))
{
return true ;
}
}
if (checkDiag(0))
return true ;
return false ;
}
void Board::displayBoard (ostream& out)
{
out << "---------------" << endl;
for ( int i=0; i< rows; i++)
{
for ( int j=0; j < cols; j++)
{
out << "|" << board[i][j];
}
out << "|" << endl;
out << "---------------" << endl;
}
}
void Board::resetBoard()
{
for (int i= 0; i<5;i++)
{
for (int j=0; j<6; j++)
{
board[i][j]== "" ;
}
}
checkerCount=0;
}
the error message i get is
.\Board.cpp(20) : error C2065: 'board' : undeclared identifier
.\Board.cpp(24) : error C2065: 'board' : undeclared identifier
.\Board.cpp(28) : error C2065: 'board' : undeclared identifier
please any help will help, im a beginner by far.
Apr 3, 2014 at 9:48pm UTC
You are missing a class qualifier from
bool checkCol(int col)
Like so:
1 2 3 4
bool Board:: checkCol( int col )
{
// Removed for brevity.
}
Edit: I have only skimmed through the rest of your code, nothing else jumped out at me but if you're still struggling, just shout :-)
Last edited on Apr 3, 2014 at 9:51pm UTC
Apr 3, 2014 at 10:17pm UTC
You'll need to check the dropChecker function alongside making the changes Lodger suggested as not all control paths return a value.
For example, if
if (column - 1 >= 0 && column - 1<8)
is not true your function returns false, but what happens when its false and you enter your for..loop code - if the
if (board[i][column - 1] == "" )
is not true then
return true ;
will not be reached and the for..loop will simply exit.
Remove the "else" from the return false; at the end of the function..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
bool Board::dropChecker(string checker, int column)
{
if (column - 1 >= 0 && column - 1<8)
{
for (int i = 5; i >= 0; i--)
{
if (board[i][column - 1] == "" )
{
board[i][column - 1] = checker;
checkerCount++;
return true ;
}
}
}
return false ;
}
Topic archived. No new replies allowed.