In grid.h, I have
#ifndef __GRID_H__
#define __GRID_H__
#include "block.h"
extern char **theBoard;
extern Block* blocks[40];
#endif
In my "board.h" file, I have (abbreviated)
#include "block.h"
class Board
{
....
char **theBoard;
Block* blocks[40];
....
}
In my "board.cpp" file, I have initialized theBoard and blocks (abbreviated)
#include "board.h"
#include "grid.h"
theBoard = new char*[18];
for(int i=0; i<18; i++){
theBoard[i] = new char[10];
for(int j=0; j<10; j++){
theBoard[i][j] = ' ';
}
}
for (int i = 0; i < 40; i++)
blocks[i] = 0;
In my "block.h" file I have (abbreviated)
class Block
{
....
char **theBoard;
Block* blocks[40];
....
}
In my "block.cc" file, I have (abbreviated)
#include "grid.h"
#include "block.h"
//somewhere in the file I try to read theBoard[1][1]
My program crashes when it tries to read theBoard[1][1] in block.cc. Placing a debugger's watch window on theBoard shows that it is pointing to garbage when the program is running instructions in block.cc
The code is like that because I'm trying to follow the instructions given in answers others have given for the same problem in similar questions posted here.
I'm trying to use the global variables declared in grid.h in every file that includes grid.h.
Can anyone tell me where I made a mistake in using global variables?
Thanks