views:

154

answers:

2

I keep getting an error on visual studio that says list iterator not decrementable: line 256

My program works fine on Linux, but the Visual Studio compiler throws this error.

Anyway, do you see what my problem is?

#include <iostream>
#include <fstream>
#include <sstream>
#include <list>

using namespace std;

int main(){

    /** create the list **/
    list<int> l;

    /** create input stream to read file **/
 ifstream inputstream("numbers.txt");

 /** read the numbers and add them to list **/
 if( inputstream.is_open() ){

  string line;
  istringstream instream;
  while( getline(inputstream, line) ){
      instream.clear();
      instream.str(line);

      /** get he five int's **/
      int one, two, three, four, five;
      instream >> one >> two >> three >> four >> five;

      /** add them to the list **/
      l.push_back(one);
      l.push_back(two);
      l.push_back(three);
      l.push_back(four);
      l.push_back(five);
  }//end while loop

 }//end if

 /** close the stream **/
 inputstream.close();

 /** display the list **/
 cout << "List Read:" << endl;
 list<int>::iterator i;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** now sort the list **/
 l.sort();

 /** display the list **/
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;

 list<int> lReversed;
 for(i=l.begin(); i != l.end(); ++i){
     lReversed.push_front(*i);
 }
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove first biggest element and display **/
 l.pop_back();
 cout << "List after removing first biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
 cout << endl;
 cout << "Sorted List (tail to head):" << endl;
    lReversed.pop_front();
    for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

    /** remove second biggest element and display **/
 l.pop_back();
 cout << "List after removing second biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;

    lReversed.pop_front();
 cout << "Sorted List (tail to head):" << endl;
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** remove third biggest element and display **/
 l.pop_back();
 cout << "List after removing third biggest element:" << endl;
 cout << "Sorted List (head to tail):" << endl;
 for( i=l.begin(); i != l.end(); ++i){
     cout << *i << " ";
 }
    cout << endl;
 cout << "Sorted List (tail to head):" << endl;
 lReversed.pop_front();
 for(i=lReversed.begin(); i!=lReversed.end(); ++i){
     cout << *i << " ";
 }
 cout << endl << endl;

 /** create frequency table **/
 const int biggest = 1000;

 //create array size of biggest element
 int arr[biggest];
 //set everything to zero
 for(int j=0; j<biggest+1; j++){
     arr[j] = 0;
 }

 //now update number of occurences
 for( i=l.begin(); i != l.end(); i++){
     arr[*i]++;
 }

 //now print the frequency table. only print where occurences greater than zero
 cout << "Final list frequency table: " << endl;
 for(int j=0; j<biggest+1; j++){
     if( arr[j] > 0 ){
         cout << j << ": " << arr[j] << " occurences" << endl;
     }
 }




    return 0;
}//end main
+1  A: 

er, what version of visual studio are you using? - anyway just copied your (143 lines) of code straight into a vs2008 project -compiled without so much as a warning!

[edit] & if i rip out the stuff where u read in 'numbers' from a file & replace simply with:

  l.push_back(1);
  l.push_back(2);
  l.push_back(3);
  l.push_back(4);
  l.push_back(5);

then in runtime vs2008 tells (me anyway):

List Read: 1 2 3 4 5

Sorted List (head to tail): 1 2 3 4 5 Sorted List (tail to head): 5 4 3 2 1

List after removing first biggest element: Sorted List (head to tail): 1 2 3 4 Sorted List (tail to head): 4 3 2 1

List after removing second biggest element: Sorted List (head to tail): 1 2 3 Sorted List (tail to head): 3 2 1

List after removing third biggest element: Sorted List (head to tail): 1 2 Sorted List (tail to head): 2 1

Final list frequency table: 1: 1 occurences 2: 1 occurences

[last flippin edit] urrgh, so to spell it out, what's basically wrong with ur code is that the latter part of the code executes (ie. things like: l.pop_back(); ) regardless of whether the file is read or not - a skool-girl error =0

& so this time, not really a MS issue,lol (glad 2 see u removed that rant from ur q, lol)

violet313
"XXX iterator is not YYY" is a runtime error. Did you try running the code?
Billy ONeal
i will admit i gave up going that far when i discovered there were'nt 256 lines of code
violet313
@violet313: That's because the error is being thrown from whithin the bowels of the standard library's list class, not the OP's code.
Billy ONeal
violet313
..of course, if my "numbers.txt" file happens not to exist then naturally when i try something like this: l.pop_back(); it all fuks up ;)
violet313
+3  A: 

You're writing out of bounds of your arr array, it causes stack corruption when running it after compiling with the debugging config in VS2008, even tells you what variables you messed up with.

The problem lies in the fact that you try to write to the length of the array, instead of the length - 1. Not only do you do try writing out of bounds, you also read it later.(Line 122, 133 and 134)

Not getting compiler errors or warnings, nor a debug assertion telling me that I'm trying to decrement a non-decrementable iterator anywhere when I run the program. The error you're seeing could just be a side effect from corrupting the stack, but I'm just guessing there.

Jacob