Vector Iterator problem in VS2005

Started by
14 comments, last by iMalc 14 years, 10 months ago
Hi

I must tell you i am not expecting anything :)

I just got this code from library and i am using it. In VC6 it working fine. in VC2005 this error. Because of more changes in STL. in VC6 iterator act like pointer.

So i am expecting to see how can i figure out this code.
Advertisement
Well, you have a pointer that points to the first element of the array. If you decrement that pointer by another element, you're out of bounds. There is no element before the first one. If you want the last, then use face.end() - 1, but that is only valid when there is at least one item in it. Just because "it runs" doesn't mean that it is valid code.

*edit* So if you aren't expecting anything, why'd you copy the code in the first place?
Quote:Original post by rozz666
Quote:Original post by nareshn2008
Hi

It is returns face.begin(). So what will be trick to reverse_iterator ?


But what do you expect? You can't get an element before the first one. That's an error.


Though that would be a runtime error, not a compile time error like claimed in the OP.
Quote:Original post by phresnel
Quote:Original post by rozz666
Quote:Original post by nareshn2008
Hi

It is returns face.begin(). So what will be trick to reverse_iterator ?


But what do you expect? You can't get an element before the first one. That's an error.


Though that would be a runtime error, not a compile time error like claimed in the OP.


It is a runtime error.

Quote:Original post by nareshn2008
...am getting some assert error.
Quote:Original post by rozz666
Quote:Original post by phresnel
Quote:Original post by rozz666
Quote:Original post by nareshn2008
Hi

It is returns face.begin(). So what will be trick to reverse_iterator ?


But what do you expect? You can't get an element before the first one. That's an error.


Though that would be a runtime error, not a compile time error like claimed in the OP.


It is a runtime error.

Quote:Original post by nareshn2008
...am getting some assert error.


My fail about claiming that the OP claimed it's a compile time error, but now I must say that the part you have quoted now does not say anything about whether it is a compile time assertion or a runtime assertion.

But what lead me to my wrong post above is that the original code

vector<int>::iterator result = ...;vector<BSP_VertexInd>::iterator prev = result - 1; // <=== this point i am getting assert error.


was invalid in case BSP_VertexInd is not an alias to int or when you don't provide a specialization of std::vector<> upon BSP_VertexInd, and did hence not even compile.
Whatever library that code is from has a bug. As you've been told, it is not allowable to decrement the begin() iterator. It might have gone unnoticed in an earlier compiler because the earlier one probably didn't go to all the effort to detect the problem for you. Instead it would have perhaps just randomly failed.
The library you mention needs to be fixed.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement