When I run this code it gives me a debug assertion failed, line 1232, vector subscript out of range.
Maze::Maze(bool verbose = false)
// verbose == true means an intro and prompts for input will be printed
{
int i, j, dimension;
if (verbose) {
cout << "Welcome to the Rat in the Maze program, where we will find a\n"
<< "path from the start cell to the end cell of a maze so that Remy\n"
<< "may escape. You will first enter the data specifying the maze.\n"
<< "After that, if escape is possible, we will show an escape path\n"
<< "Enter the number of rows and columns of the maze: " << endl;
}
cin >> dimension;
size = dimension+2; // add the hedge rows and columns
if (verbose)
cout << "Enter the row and column indices of the start: " << endl;
cin >> start;
if (verbose)
cout << "Enter the row and column indices of the exit: " << endl;
cin >> exitpos;
/Here is where I got the error:/
M.reserve(size);
for (i = 0; i < size; i++) {
M[i].reserve(size);
for (j = 0; j < size; ++j)
M[i][j] = WALL;
}
if(verbose) {
cout << "For each row, enter the column indices of the open squares\n";
cout << "Terminate the row input with a non-positive value" << endl;
}
for (i = 1; i <= size-2; i++) {
if (verbose)
cout << "Row " << i << ": ";
cin >> j;
assert(j < 1 || 1 <= j && j <= size-2);
while (j > 0){
M[i][j] = OPEN;
cin >> j;
assert(j < 1 || 1 <= j && j <= size-2);
};
}
if (!(M[start.row][start.col] == OPEN))
M[start.row][start.col] = OPEN;
}
M is an object of stateTable, which defined as below:
typedef vector<vector<state> > stateTable;
There is no logic error in the code but I got this error, and one thing I know is our instructor wrote and compile this code under Linux environment while I'm doing that under VC environment. Is that the reason?