Sign in to follow this  
nareshn2008

Vector Iterator problem in VS2005

Recommended Posts

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<int>::iterator result = find(face.begin(),face.end(),v1); vector<int>::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]

Share this post


Link to post
Share on other sites
I don't see how that would compile, a vector<int>::iterator cannot be converted to a vector<BSP_VertexInd>::iterator (unless BSP_VertexInd is a typedef for int, but that would be confusing)

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
I don't see how that would compile, a vector<int>::iterator cannot be converted to a vector<BSP_VertexInd>::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 :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
I don't see how that would compile, a vector<int>::iterator cannot be converted to a vector<BSP_VertexInd>::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 ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by nareshn2008
Quote:
Original post by Codeka
I don't see how that would compile, a vector<int>::iterator cannot be converted to a vector<BSP_VertexInd>::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 :)

Share this post


Link to post
Share on other sites
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<int>::iterator cannot be converted to a vector<BSP_VertexInd>::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<BSP_VertexInd>::iterator result =
find(face.m_verts.begin(),face.m_verts.end(),v1);


vector<BSP_VertexInd>::iterator prev = result - 1; // << here is error

BSP_VertexInd is some sort of structure .


Share this post


Link to post
Share on other sites
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<int>::iterator cannot be converted to a vector<BSP_VertexInd>::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<BSP_VertexInd>::iterator result =
find(face.m_verts.begin(),face.m_verts.end(),v1);


vector<BSP_VertexInd>::iterator prev = result - 1; // << here is error

BSP_VertexInd is some sort of structure .


Did you check what std::find returns?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this