954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Vector Iterator Not Dereferenceable ( Debug Assertion Failed, Visual C++ )

I am trying to run what i thought was a pretty basic program to solve a problem concerning strings (besides the point). It turns out that my algorithm works properly but i am stuck on a detail; the following segment of code is the problem:

int cases;
	string line;
	vector<string> frags, results;

	cin >> cases;

	while ( cases > 0 ) {
		
		while (cin >> line){

			frags.push_back(line);
			line = "";
		}

		results.push_back( find_original(frags) ); 
                          //find_original does not modify frags, and returns a string 

		frags.clear();

		cases--;
	}


the problem occurs when i input a value of 'cases' that is higher than 1. the error occurs during the second run-through when the program hits the inner while loop. I get a debugger error telling me that a vector iterator is not dereferenceable. Any help is greatly appreciated.

Coffee_Table
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

I don't think you need the second while loop.Also post your find_original implementation.

caut_baia
Posting Whiz
387 posts since Apr 2010
Reputation Points: 25
Solved Threads: 49
 

@caut baia - the inner while loop is so that i can read in an arbitrary amount of strings (which is essential to the problem) until i terminate with an EOF character (^Z). posting find_original will not help because i am not passing frags by reference nor am i modifying it at all, other than with frags.clear() . Further, i ran a number of watches while debugging and there didn't seem to be any funny business. The key point is that the program went fine if i let cases = 1. Even when i let cases >= 1, the first run-thru of the loop worked. only when i got to the second run-thru did i get this vector iterator error.

Coffee_Table
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

I would have to agree with caut_baia that we need to see find_original and also some more of your code.

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

problem solved! turns out that "not deferenceable vector iterator" error was a result of a more primary problem with the program; Everything works smoothly until the second run-thru...why? well, here's what was happening: on the first run-thru, the progran would clear the contents of frags, leaving it empty. on the second run-thru, the inner while loop would be skipped because the input stream's last input was the EOF marker! then the program would try to pass the empty frags to find_original, creating all sorts of problems...thus, all i needed to do was add the single line:

frags.clear();
                cin.clear()
		cases--;
	}


this flushed the input stream and allowed the program to function normally.

Coffee_Table
Newbie Poster
9 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: