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

list iterator not dereferencable

The following is a function that compiles just fine, but when I run it, I get a "Debug Assertion Failed" and it says "list iterator not dereferencable". As far as I can tell the error occurs at whileIter++. I've looked at examples of iterators and how they are used, and can't figure out what I'm doing wrong.

CString findImage(CString fileName, list<record> csvFile)
{
	std::list<record> tmpCsvFile = csvFile;
	std::list<record>::iterator tmpIterator = tmpCsvFile.begin();
	list<record>::iterator whileIter = tmpCsvFile.begin();
fileName = fileName+".txt";
		
		while (whileIter != tmpCsvFile.end())
		{
		      whileIter++;
		if (fileName == (whileIter->fileName).c_str())
			break;
			
		}
		
return whileIter->characters.c_str();
}


Basically there is a list of records ( a struct with a few members, one of which is fileName). The function should compare the fileName in each record of the list, with the fileName that was passed as a parameter to the function in the hope of finding two that are equal.

Thanks in advance

Natique
Light Poster
28 posts since Feb 2008
Reputation Points: 11
Solved Threads: 0
 

Your problem is inside the while loop.

while (whileIter != tmpCsvFile.end())
		{
		      whileIter++;
		if (fileName == (whileIter->fileName).c_str())
			break;
			
		}


You have advanced the iterator (possibly to the end) before you try to test the filename. Put the fileName test (with the break) before the iterator advancement.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You