Vector subscript out of range - instantiation error.

Hi there,

I'm trying to initialize a class with a 2D matrix subclass. I've been trying to use a double vector (vector< vector<Cell>> maze_cell) and then access it like a 2d array - I'm new to vectors and I think this may be the source of my problem.

When I debug my program, the runtime error: vector subscript out of range occurs as soon as I attempt to access the first entity of the matrix. From what I've researched about this runtime error, it should occur when I try and access an element outside of the memory - how can this occur when:
1) I try and access the [0][0] location.
2: Vectors are supposed to dynamically allocate memory when I need it.

what follows is my code, hopefully commented enough :)

main.cpp:
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
#include "Cell.h"
#include "Maze.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

#define infile "C:\\maze.txt"
int main()
{
	string temp;
	char char_array[100][100];		

	
	ifstream fin(infile,ios::app);

	cout << "This is the current maze:" << endl; //output the maze stored in the infile adress
	cout << fin.rdbuf();
	cout << endl;
	
	fin.seekg(0, ios::beg); //reset file pointer to the beginning of the file.
	fin.clear();

	int dimention, i, j;

	getline(fin, temp); // get the first line - this should be the dimention scale.
	dimention = atoi(temp.c_str());

	for(j=0;j<100;j++)
	{
		for(i=0;i<100;i++)
		{
			char_array[i][j] = 'N';
		}
	}

	for(j=0;j<dimention;j++)
	{
		for(i=0;i<dimention;i++)
		{
			getline(fin, temp, ' ');
			char_array[i][j] = char(temp.c_str());// this appears in my array as 0 - I don't know why 
                                                              //- shouldn't I be taking in the char from the temp string 
                                                              //- it works for dimention
		}
	}
	


	Maze M(dimention);  // this is as far as the compiler gets - this function is in Maze.h
	M.setmaze(char_array);
	M.print();

	return 0;
	
}


Maze.h - the problem occurs in this:
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
#ifndef MAZE_H
#define MAZE_H
#include "Cell.h"
#include <iostream>
#include <vector>
using namespace std;

class Maze
{
public:
	Maze(int dimention_value);			
	void setmaze(char maze_array[100][100]);
	int setstart(int x_coord, int y_coord);
	Maze dead_end_fill();
	void print();

private:
	int dimention;
	vector<vector<Cell>> maze_cell;	// vector of vector of type Cells - each stores a block of the maze.

};
Maze::Maze(int dimention_value)	//initialisation function
{
	int i,j;
	dimention = dimention_value;
	for (i=0;i<dimention;i++)
	{
		for(j=0;j<dimention;j++) 
		{
			maze_cell[i][j].setstate('0'); //***runtime error here***	//set all the cells to 0 (mainly for debugging)
			maze_cell[i][j].setxy(i,j);		//initialise all the coordinate values for each cell.
		}
	}
}


void Maze::setmaze(char maze_array[100][100])// function to shift dimention*dimention chars of the maze_array into the Maze class.
{
	int i,j;
	for(i=0;i<dimention;i++)for(j=0;j<dimention;j++)
	{
		maze_cell[i][j].setstate(maze_array[i][j]);
	}
}

int Maze::setstart (int x_coord, int y_coord) //function to set the start point of the maze.
{
	if(maze_cell[x_coord][y_coord].free())
	{
		maze_cell[x_coord][y_coord].setstate('s'); //set point to s
		return 1;									// return 1 if sucessful
	}
	return 0;	//return 0 if unsucessful (tried to start in a wall)
}

void Maze::print()		// function to print the maze class
{
	int i, j;
	for(i=0;i<dimention;i++)
	{
		for(j=0;j<dimention;j++)
		{
			cout << maze_cell[i][j].get_state();
		}
		cout << endl;
	}
}
#endif 


finally the Cell.h file - I think this is ok :D :
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
#ifndef CELL_H
#define CELL_H

class Cell
{
public:
	int get_x() {return x;};
	int get_y() {return y;};				// get_"variable"() -> returns the variable
	char get_state() {return state;};
	int wall(); //returns 1 if the block is a wall
	int free();	//returns 1 if the block is a free space
	int goal();	//returns 1 if the block is the goal
	int start();//returns 1 if the block is the start
	void setx(int x_val) {x = x_val;};
	void sety(int y_val) {y = y_val;};
	void setxy(int x_val, int y_val) {x = x_val; y = y_val;};	//set"variable"(X) -> variable gets value of X
	void setstate(char c) {state = c;};

private: 
	int x,y;		// variables
	char state;
};

int Cell::wall()
{
	if(state == 'x') return 1;
	else return 0;
}

int Cell::free()
{
	if(state =='p') return 1;
	else return 0;
}

int Cell::goal()
{
	if(state =='g') return 1;
	else return 0;
}
int Cell::start()
{
	if(state =='s') return 1;
	else return 0;
}

#endif


What is the source of this runtime error?

As you may have noticed above I'm also having trouble getting the characters to put into my initial 100x100 array - they show up as all '0' when I'm expecting Xs and Ps - if anyone has a fix I'd be grateful.


Thanks for any help you can provide - I've been tearing my hair out over this.

Nick

p.s. first time poster - be gentle!
Turns out I hadn't been declaring the size of the vector :/

here's the fix I used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Maze::Maze(int dimention_value)	//initialisation function
{
	int i,j;
	dimention = dimention_value;
	maze_cell.resize(dimention);
		
	for (i=0;i<dimention;i++)
	{
		maze_cell[i].resize(dimention);
		for(j=0;j<dimention;j++) 
		{
			maze_cell[i][j].setstate('0');	//set all the cells to 0 (mainly for debugging)
			maze_cell[i][j].setxy(i,j);		//initialise all the coordinate values for each cell.
		}
	}
}



Any help on my other problem would still vastly be appreciated!

Nick
Don't declare the size of a vector. Use "push_back(...)": http://www.cplusplus.com/reference/stl/vector/push_back/
Using this member function makes loading arrays so much easier that I tend to use vectors now even when they're completly unnecessary.
I see how push_back(...) works - however, with the 2d array i'd rather size it and forget.

As i'll be manipulating it the same way as below throughout the program - It's also more what i'm used to and comfortable with.

is there anything detrimental about the way i have declared above?

Also managed to fix my second problem:
1
2
3
4
5
6
7
8
9
	for(j=0;j<dimention;j++)
	{
		for(i=0;i<dimention;i++)
		{
			fin.get(char_array[i][j]);
			fin.get(junk); //kill space char (at end should kill the end line char)
		}

	}


Thanks again,
Nick
Topic archived. No new replies allowed.