944,004 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4967
  • C++ RSS
Sep 26th, 2009
0

list iterator not dereferencable

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Natique is offline Offline
28 posts
since Feb 2008
Sep 30th, 2009
0

Re: list iterator not dereferencable

Your problem is inside the while loop.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help With this program
Next Thread in C++ Forum Timeline: Classes and Linked Nodes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC