🔒 Locked
Vector Iterator problem in VS2005
Advertisement
Original Post
Hi I got some strange problem. I have code in visual c++ 6. and i upgrade it to VS 2005 compiler . Since this days STL changed lot of things i am getting some assert error. Here my code look like this. // look for v1 vector::iterator result = find(face.begin(),face.end(),v1); vector::iterator prev = result - 1; // <=== this point i am getting assert error. Can anyone tell me why it gives me error ? [Edited by - nareshn2008 on May 29, 2009 5:04:39 AM]
Quote:
Original post by Codeka
I don't see how that would compile, a vector::iterator cannot be converted to a vector::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)
Sorry i was miss it. I just want to show simple example :)
Quote:
Original post by Codeka
I don't see how that would compile, a vector::iterator cannot be converted to a vector::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)
#include <vector>struct BSP_VertexInd {};namespace std { template <> struct vector<BSP_VertexInd> { struct iterator { template <typename T> iterator (T const &) {} }; };}using std::vector;vector<int>::iterator result;vector<BSP_VertexInd>::iterator prev = result - 1; // <=== this point i am getting assert error.int main () {}Compiles fine, no typedef harmed ;)
Quote:
Original post by nareshn2008 Quote:
Original post by Codeka
I don't see how that would compile, a vector::iterator cannot be converted to a vector::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)
Sorry i was miss it. I just want to show simple example :)
We will be able to help you if you post a real, preferably minimal, testcase :)
Quote:
Original post by phresnel Quote:
Original post by nareshn2008 Quote:
Original post by Codeka
I don't see how that would compile, a vector::iterator cannot be converted to a vector::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)
Sorry i was miss it. I just want to show simple example :)
We will be able to help you if you post a real, preferably minimal, testcase :)
Hi phresnel
Thanks for reply :)
This is real code.
vector
find(face.m_verts.begin(),face.m_verts.end(),v1);
vector
BSP_VertexInd is some sort of structure .
Quote:
Original post by nareshn2008 Quote:
Original post by phresnel Quote:
Original post by nareshn2008 Quote:
Original post by Codeka
I don't see how that would compile, a vector::iterator cannot be converted to a vector::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)
Sorry i was miss it. I just want to show simple example :)
We will be able to help you if you post a real, preferably minimal, testcase :)
Hi phresnel
Thanks for reply :)
This is real code.
vector::iterator result =
find(face.m_verts.begin(),face.m_verts.end(),v1);
vector::iterator prev = result - 1; // << here is error
BSP_VertexInd is some sort of structure .
Did you check what std::find returns?
Hi
It is returns face.begin(). So what will be trick to reverse_iterator ?
It is returns face.begin(). So what will be trick to reverse_iterator ?
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.
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.
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?
*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.
The library you mention needs to be fixed.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement