2

I am having problems building a library in VC9, but which was previously successfully built in VC6. Here is a part of the code:

size_t pos2find;
pos2find = J; 
std::vector<size_t>::iterator it(&pos2find);

And here is the error:

    error C2664: 'std::_Vector_iterator<_Ty,_Alloc>::_Vector_iterator(const std::_Vector_iterator<_Ty,_Alloc> &)' : cannot convert parameter 1 from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc> &'
    1>        with
    1>        [
    1>            _Ty=size_t,
    1>            _Alloc=std::allocator<size_t>
    1>        ]
    1>        Reason: cannot convert from 'size_t *' to 'const std::_Vector_iterator<_Ty,_Alloc>'

1>        with
1>        [
1>            _Ty=size_t,
1>            _Alloc=std::allocator<size_t>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

I appreciate any help.

EDIT: The code is from an open source library called "surfit", it is not my code, so i guess it's something that changed in the standard of the vector class. The iterator is then used in another std function:

std::vector<size_t>::iterator * ptr_from = fault->sort_by_first_begin;
std::vector<size_t>::iterator * ptr;
ptr = std::lower_bound(ptr_from, 
               fault->sort_by_first_end, 
               it, 
               ptr_size_t_less);

EDIT: I think i found a solution. After looking into std::lower_bound() :

template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,
                                const T& value, Compare comp );

Return iterator to lower bound
Returns an iterator pointing to the first element in the sorted range [first,last) which does not compare less than value. The comparison is done using either operator< for the first version, or comp for the second.

For the function to yield the expected result, the elements in the range shall already be ordered according to the same criterion (operator< or comp).

With this i just eliminated the it iterator and used lower_bound(first, last, &pos2find, comp);

2
  • 1
    As a general hint: msvc6 is so broken and pre-standard that a whole lot of code written for it will not compile with a standards compliant compiler. It did not even support proper for loop variable scoping until one of the later service packs.
    – PlasmaHH
    Aug 22, 2011 at 12:27
  • I figured as much. But thought maybe there is a workaround for this case.
    – Adrian
    Aug 22, 2011 at 12:41

4 Answers 4

4

An iterator into a vector isn't guarantied to be a raw pointer. In VS6 vector<T>::iterator happened to be a T* so you could initialize the iterator with a T*. In later versions of VS the implementation of vector<T>::iterator changed to be a class type (as it's entitled to be) so code that made bad assumptions now fails to compile.

In any case the code you posted is buggy even if iterator is a T* since the pointer supplied is not a pointer into the vector.

4

Well the posted code makes no sense at all even in VC6. But maybe you are looking for something like this

std::vector<size_t>::iterator it = some_vector.begin() + pos2find;

If not then you better post a bit more code, so we can see how you trying to use the iterator.

2
  • I wish that was the case, but the iterator is then used in a more complex method from the std.
    – Adrian
    Aug 22, 2011 at 12:55
  • @Adrian, see my new attempt to interpret this.
    – john
    Aug 22, 2011 at 13:12
2

The original code relies on an implementation detail that isn't guaranteed by the standard, i.e. the fact that std::vector iterators could be implemented as pointers. I'm not sure whether this is still the case, but the implementation changed to a specific class template in later versions of Visual C++.

I'm afraid that you have to go through all your code and fix things, more so since, as it has been already pointed out, your example is buggy even taking into account what I just wrote.

Beware that with pointers you can use 0 to mean "iterator not set", which would be different from assigning some_vector.end(). If you encounter a similar situation I suggest that you use Boost.Optional to wrap your iterator.

Been there, done that (but with a different compiler).

1

The following seems to be what's intended

std::vector<size_t> tmp;
tmp.push_back(J);
std::vector<size_t>::iterator it = tmp.begin();

std::vector<size_t>::iterator * ptr_from = fault->sort_by_first_begin;
std::vector<size_t>::iterator * ptr;
ptr = std::lower_bound(ptr_from, 
               fault->sort_by_first_end, 
               it, 
               ptr_size_t_less);

Crazy code (a sorted array of iterators, really?), and untested by me.

1
  • Yeah, it's a good idea, but just found that i can replace it with &pos2find. Thanks for the support.
    – Adrian
    Aug 22, 2011 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.