vector subscript out of range

Hello, I keep getting this "Debug Assertion Failed" error that says:
expression: vector subscript out of range

I tried to make the loop the same as the vector size, but I'm still getting the same errors. Could anyone tell me what I'm doing wrong or how to fix this error?

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
	void Grid::output(ostream & out) 
        {
		
		vector<vector<int>> grid(4);
			
		int rows, columns;
		out << "    0 1 2 3 " << endl;
		out << "  +---------+" <<  endl;

	for( rows=0; rows<grid.size(); ++rows ) // make each row
		{
			out << rows << " | ";		
			for( columns=0; columns<grid.size(); ++columns ) //make each column
			{
				if(startPosR_ == NULL || startPosC_ == NULL || endPosR_ == NULL || endPosC_ == NULL)
					
					out << "O" << " ";
			
				else
					out << "X" << " ";
				
			}
			out << "| ";	
			out << endl; 
		}
		out << "  +---------+" <<  endl;
	}
The code you posted doesn't access the (incorrect) vector, so it can't be the one that's causing the assertion.
Last edited on
It's the only vector that I'm using in the file though.
Also, when I went to debug, the assertion happened once I called that specific function.

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
#include <iostream>
#include "Grid.h"
#include "DUPoint.h"
#include <vector>
#include <sstream>
#include <fstream>

using namespace std;
		
//constructors
		
		//initializes Grid object
		Grid::Grid() { }

		Grid::Grid(DUPoint start, DUPoint end) 
		{
			startPos_ = start;
			endPos_ = end;
		}

		//Grid::Grid( const Grid & a ) 
		//{
		//	startPos_ = a.getStart ;
		//	endPos_ = a.getEnd ;
		//}

//inspectors

		//gets start position
		DUPoint Grid::getStart(DUPoint start) const 
		{
			return startPos_;
		}

		//gets end position
		DUPoint Grid::getEnd(DUPoint end) const 
		{
			return endPos_;
		}

//mutators

		//sets start position
		void Grid::setStart(DUPoint start) 
		{
			startPos_ = start;
		}
		
		//sets end position
		void Grid::setEnd(DUPoint end) 
		{
			endPos_ = end;
		}

		//checks if userMove is legal
		bool Grid::isLegalMove(int startPosRow, int startPosColumn,int endPosRow,int endPosColumn) 
		{
		
			if ( startPosRow < 0 || startPosRow > 3 
				 || startPosColumn < 0 || startPosColumn > 3
				 || endPosRow < 0 || endPosRow > 3 )
				 return false;
			else 
				return true;
		}
		
		//apply userMove
		void Grid::applyMove(int startPosRow, int startPosColumn, int endPosRow, int endPosColumn)
		{
			if (isLegalMove(startPosRow, startPosColumn, endPosRow, endPosColumn)) {
			grid_[startPosRow][startPosColumn];
			grid_[endPosRow][endPosColumn];
		}else 
			exit(1);		
		}
	
		//sets startPosition Row n Column, endposition Row n Column
		void Grid::setGrids(int startPosRow, int startPosColumn, int endPosRow, int endPosColumn)
		{
			startPosR_ = startPosRow;
			startPosC_ = startPosColumn;
			endPosC_ = endPosColumn;
			endPosR_ = endPosRow;
		}

//facilitators
	
	//Displays grid
	void Grid::output(ostream & out) {
		
		vector<vector<int>> grid(4);
				
		int rows, columns;
		out << "    0 1 2 3 " << endl;
		out << "  +---------+" <<  endl;

		for( rows=0; rows< grid.size(); ++rows ) // make each row
		{
			out << rows << " | ";		
			for( columns=0; columns< grid.size(); ++columns ) //make each column
			{
				if(grid.empty())
					
					out << "X" << " ";
			
				else
					out << "X" << " ";
				
			}
			out << "| ";	
			out << endl; 
		}
		out << "  +---------+" <<  endl;
	}

	//overloads << operator
	ostream & operator << ( ostream & out,  Grid & p)
	{

		p.output(out);
		return out;
	}



testfile.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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "DUPoint.h"
#include "Grid.h"
using namespace std;

int main() {

	Grid bacon;
	DUPoint userInput;
	DUPoint userMove1,
			userMove2;
	int cont;		
	ofstream fout("grid.txt");
		
		bacon.output(fout);
		bacon.output(cout);

		cout << "Which peg should be removed to start?" <<  endl;
		cin >> userInput;
	

	do 
	{
		cin.clear();

		cout << "Enter move: " << endl;

		cin >> userMove1 >> userMove2;
		bacon.isLegalMove(userMove1.getX(),userMove1.getY(),userMove2.getX(),userMove2.getY());
		bacon.applyMove(userMove1.getX(),userMove1.getY(),userMove2.getX(),userMove2.getY());
		bacon.output(fout);
		bacon.output(cout);

		cin.clear();
		
		cout << "Do you wish to continue? (0 for YES, 1 for NO)" << endl;
		
		cin >> cont;

	}while( cont == 0 );


	return 0;
}

grid_ is probably never initialized. Lines 71 and 72 either crash, or don't crash but don't do anything at all.
Topic archived. No new replies allowed.