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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.