我在VC9中构建库时遇到了问题,但之前成功构建于VC6中。 下面是代码的一部分:使用C++中的指针初始化迭代器
size_t pos2find;
pos2find = J;
std::vector<size_t>::iterator it(&pos2find);
这里是错误:
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
我感谢所有帮助。
编辑: 该代码来自一个名为“surfit”的开源库,它不是我的代码,所以我猜这是在矢量类的标准中发生了变化。 迭代器,然后在另一个STD功能使用:
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);
编辑: 我想我找到了解决办法。寻找到的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).
有了这个,我只是消除了它的迭代器和使用LOWER_BOUND(第一,最后,& pos2find,COMP);
作为一个常规提示:msvc6是如此的破坏和预标准,以致为它编写的大量代码将不能用符合标准的编译器进行编译。它甚至不支持正确的循环变量范围,直到后来的某个Service Pack。 – PlasmaHH
我想尽可能多。但是,也许有人认为这种情况有一个解决方法。 – Adrian