5 Replies - 1159 Views - Last Post: 06 March 2012 - 06:32 PM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 04:04 PM

So I almost got through the end of the chapter without a program error, but now it seems the following problem may be program/compiler related.

The programs runs and displays the result it's supposed to. But then a different pop up from microsft's visual c++ 2010 comes up. I have attached an image as well.



Source code
// This program provides a simple iterator demonstration.
#include <iostream> 
#include <vector>			// Include the vector header 
using namespace std;

int main() 
{ 
	int count;				// Loop counter 
	
	// Define a vector object. 
	vector<int> vect; 

	// Define an iterator object. 
	vector<int>::iterator iter;

	// Use push back to push values into the vector. 
	for (count = 0; count < 10; count++) 
		vect.push_back(count);	//vect.push_back(count+1); will do 1 to 19, rather than 0 to 9

	// Step the iterator through the vector, and use 
	// it to display the vector's contents. 
	cout << "Here are the values in vect: "; 
	for (iter = vect.begin(); iter < vect.end(); iter++) 
	{ 
		cout << *iter << " "; 
	} 

	// Step the iterator through the vector backwards. 
	cout << "\nand here they are backwards: "; 
	for (iter = vect.end() - 1; iter >= vect.begin(); iter--) 
	{ 
		cout << *iter << " "; 
	} 
	cout << endl;

	system("pause");
	return 0;
} 

Attached image(s)

  • Attached Image

This post has been edited by mgrex: 06 March 2012 - 05:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Visual C++ 2010 abort error: iterator not decrementable, etc.

#2 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 04:35 PM

I can find no reason that this should fail from the perspective of the STL and Standard Library. .begin and .end return bidirectional iterators, so this issue must be from VS itself. I'll keep looking into it.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 04:42 PM

Be careful what relational operator you use when comparing iterators. Use the != operator when comparing your iterator to the front or to the end.


i.e. this
for (iter = vect.begin(); iter < vect.end(); iter++) 



should be this
for (iter = vect.begin(); iter != vect.end(); iter++) 


Was This Post Helpful? 2
  • +
  • -

#4 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 05:18 PM

Unfortunately, doing that resulted in the same error.
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is online

  • Engineer
  • member icon

Reputation: 1049
  • View blog
  • Posts: 4,458
  • Joined: 09-June 09

Re: Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 05:43 PM

You need to fix the operator in the other for loop as well
Was This Post Helpful? 1
  • +
  • -

#6 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Visual C++ 2010 abort error: iterator not decrementable, etc.

Posted 06 March 2012 - 06:32 PM

Awesome, that did the trick. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1