Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, in the following code, I'm getting the error mentioned in the title. Here's the code:

C++
typedef struct node		// this acts as the vertex of graph
{
	string name;		// stores the unique name of the vertex
	int value;			// stores the integer data saved in the vertex
} node;

class graph
{
private:
	vector< vector<node> > adjList;		// 2D vector to store the adjencey list of the graph

public:
	graph()								// default constructor
	{
		adjList.resize(0);
	}void printBFSTrav()
	{
		stack<node> stck;
		vector<string> visited;
		vector<node> result;
		//result.resize(adjList.size());

		stck.push(adjList[0][0]);
		visited.push_back(adjList[0][0].name);

		node current;
		int i=0, vTrav=0;
		while(!stck.empty())
		{
			current = stck.top();
			result.push_back( current );
			stck.pop();

			i=0;
			for(int size=adjList.size(); i<size && adjList[0][i].name != current.name; ++i);

			for(int j=1, size=adjList[i].size(); j<size; ++j)
			{
				for(vTrav=0; (vTrav < visited.size()) && (visited[vTrav] != adjList[i][j].name); ++vTrav);

				if(vTrav!=visited.size())
					continue;

				stck.push( adjList[i][j] );
				visited.push_back( adjList[i][j].name );
			}
		}

		for(int i=0; i<result.size(); ++i)
			cout << endl << result[i].name << "(" << result[i].value << ")";  //************************
	}


I get the error when this line:

for(int size=adjList.size(); i<size mode="hold">
gets executed third time. All vector indexes are within the range AFAIK.
Posted
Comments
Sergey Alexandrovich Kryukov 26-Apr-13 11:16am    
Do it under the debugger. Before the exception is thrown, examine the values of all the relevant variables. You will soon find out what's wrong. That's it.
—SA

You are reusing a variable for 2 purposes:
C++
for(int size=adjList.size(); i < size && adjList[0][i].name != current.name; ++i)
  for(int j=1, size=adjList[i].size(); j<size; ++j) ...

The variable size is defined in the outer loop (I might add, in an unusual fashion), to apparently be the outer loop size ... then you redefine the same variable as the inner loop size. When the inner loop completes, you go to the second iteration of the outer loop, but the size value is not reset properly.

There may be other errors (I didn't look past this), but you want to move the "int size=..." outside the loops and I would suggest:

C++
int isize = adjList.size();    // defined outside the outer loop
// ...
int jsize = adjList[i].size(); // defined inside the outer loop, outside the inner loop
 
Share this answer
 
v2
Comments
nv3 27-Apr-13 3:27am    
Very well observed. 5+
In the statement
C++
for (int size=adjList.size();
            i < size && adjList[0][i].name != current.name;
                     ++i);

you actually meant: adjList [i][0]. size is the dimension of the "outer" index, which is the first one in the notation.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900