list iterator not dereferencable

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 17
Reputation: Natique is an unknown quantity at this point 
Solved Threads: 0
Natique Natique is offline Offline
Newbie Poster

list iterator not dereferencable

 
0
  #1
Sep 26th, 2009
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.

  1. CString findImage(CString fileName, list<record> csvFile)
  2. {
  3. std::list<record> tmpCsvFile = csvFile;
  4. std::list<record>::iterator tmpIterator = tmpCsvFile.begin();
  5. list<record>::iterator whileIter = tmpCsvFile.begin();
  6. fileName = fileName+".txt";
  7.  
  8. while (whileIter != tmpCsvFile.end())
  9. {
  10. whileIter++;
  11. if (fileName == (whileIter->fileName).c_str())
  12. break;
  13.  
  14. }
  15.  
  16. return whileIter->characters.c_str();
  17. }

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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: list iterator not dereferencable

 
0
  #2
Sep 30th, 2009
Your problem is inside the while loop.

  1. while (whileIter != tmpCsvFile.end())
  2. {
  3. whileIter++;
  4. if (fileName == (whileIter->fileName).c_str())
  5. break;
  6.  
  7. }

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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC