Char Pointers and the Undeclared Identifer Error
Mar 30, 2014 at 11:16am UTC
I'm getting the following error when I run my code below, "error C2065: 'board' : undeclared identifier". I believe my complier is complaining saying board hasn't been given a value, and your trying to call it with, "DisplayBoard()" function. Is this a correct understanding of that error?
Is there a way to modify my code so that I could declare board outside my while loop?
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
#include <iostream>
#include <fstream>
using namespace std;
void DisplayBoard(char ** myBoard, int B_Height, int B_Width);
int main(){
fstream inFile;
int Size_Width, Size_Height;
char ** board;
inFile.open("maze.txt" );
while (!inFile.eof()){
inFile >> Size_Height;
inFile >> Size_Width;
board = new char *[Size_Height];
for (int i = 0; i < Size_Height; i++){
board[i] = new char [Size_Width];
}
for (int j = 0; j < Size_Height; j++){
for (int k = 0; k < Size_Width; k++){
inFile >> board[j][k];
}
}
}
inFile.close();
//Error here saying, "error C2065: 'board' : undeclared identifier
DisplayBoard(board, Size_Height, Size_Width);
//Side note, is the correct syntax to delete my char pointer ?
//delete [] board;
system("pause" );
return 0;
}
void DisplayBoard(char ** myBoard, int B_Height, int B_Width){
for (int y = 0; y < B_Height; y++){
for (int x = 0; x < B_Width; x++){
cout << myBoard[y][x];
}
cout << endl;
}
}
"maze.txt" (Note: this didn't copy over 100% correct, don't know why, not the issue here though.)
11 10
XXXXXXXXXX
XX...tX..X
XX.XXXX.XX
XX.......X
XXXXXXX.XX
X....XX.XX
X.XX.....X
X...XX.XXX
XXX..XXXXX
XXXX....XX
XXXXXXXXXX
Last edited on Mar 30, 2014 at 11:19am UTC
Mar 30, 2014 at 9:34pm UTC
Update: I tried to run my code today and my complier no longer had that error. I don't know why. Also removed my while loop, it wasn't working as intended.
Is the following syntax correct to delete my pointer to a pointer board?
delete [] board;
Working code:
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
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
void DisplayBoard(char ** myBoard, int B_Height, int B_Width);
int main(){
fstream inFile;
int Size_Width, Size_Height;
static char ** board;
inFile.open("maze.txt" );
inFile >> Size_Height;
inFile >> Size_Width;
board = new char *[Size_Height];
for (int i = 0; i < Size_Height; i++){
board[i] = new char [Size_Width];
}
for (int j = 0; j < Size_Height; j++){
for (int k = 0; k < Size_Width; k++){
inFile >> board[j][k];
}
}
inFile.close();
DisplayBoard(board, Size_Height, Size_Width);
system("pause" );
return 0;
}
void DisplayBoard(char ** myBoard, int B_Height, int B_Width){
for (int y = 0; y < B_Height; y++){
for (int x = 0; x < B_Width; x++){
cout << myBoard[y][x];
}
cout << endl;
}
}
Last edited on Mar 30, 2014 at 9:35pm UTC
Topic archived. No new replies allowed.