STL 迁移问题(VS 2003 -> 2005)
我刚刚将一个项目从 Visual Studio 2003 转换为 2005,虽然其中大部分“转换”得很好,但我在以下行中遇到了一系列 STL 错误:
void SomeFn( std::vector<CSomeObject*>::iterator it,
std::vector<CSomeObject*>::iterator itBegin = NULL,
std::vector<CSomeObject*>::iterator itEnd = NULL );
Visual Studio 错误如下:
c:\<path>\Headerfile.h(20) : error C2440: 'default argument' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=CObject *,
_Alloc=std::allocator<CObject *>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
我看不出有什么问题该代码在 VS 2003 中完美运行。有什么想法吗?
I have just converted a project from Visual Studio 2003 to 2005 and although most of it 'converted' fine, I have a series of STL errors from the following line:
void SomeFn( std::vector<CSomeObject*>::iterator it,
std::vector<CSomeObject*>::iterator itBegin = NULL,
std::vector<CSomeObject*>::iterator itEnd = NULL );
The Visual Studio error is as follows:
c:\<path>\Headerfile.h(20) : error C2440: 'default argument' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=CObject *,
_Alloc=std::allocator<CObject *>
]
No constructor could take the source type, or constructor overload resolution was ambiguous
I can't see anything wrong with that code and it worked perfectly in VS 2003. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的程序不正确,因为 NULL 无法转换为迭代器。 我真的不知道你希望这些迭代器被初始化为什么。 如果您需要保证迭代器不在容器中但仍然“有效”,则可以使用默认构造函数:
但是请注意,如果您这样做,
it
,itBegin
和itEnd
将无法以有意义的方式进行比较! 只有从给定容器获得的迭代器才有意义。 最后,我建议不要使用itBegin
和itEnd
的默认值。 如果您确实不需要这些,请创建另一个不带参数的函数并做一些有意义的事情。 即:您的程序的另一个问题是使用向量来存储指针。 这实在是不安全。 确保在未先删除元素的情况下永远不会从向量中删除元素。 您可能还会遇到复制对象的算法的问题。 最好在向量中使用智能指针。
Your program is incorrect as NULL cannot be converted as an iterator. I don't really know what you want these iterators to be initialized as. If you need an iterator guarantied not to be in a container but to be still "valid", you can use a default-constructor:
Note, however, that if you do so,
it
,itBegin
anditEnd
won't be comparable in a meaningful way! Only iterators obtained from a given container are comparable meaningfully. In the end, I would recommend against using defaults values foritBegin
anditEnd
. If you really need to not have these, create another function without the arguments and do something meaningful. i.e.:Another problem of your program is the use of a vector to store pointers. This is really unsafe. Make sure you never erase elements from the vector without deleting the element first. You might also have problems with algorithms copying objects around. It is better to use smart pointers in vectors.
2003 年,
std::vector::iterator
只是T *
。 在 2005 年,至少在调试模式下,它是一个类,因此您不能使用NULL
作为其值(NULL
解析为0
>)。您可以使用默认构造的迭代器来代替:
In 2003,
std::vector<T>::iterator
is justT *
. In 2005, at least in debug mode, it is a class, and hence you can't useNULL
for its value (NULL
resolves to0
).You can use a default-constructed iterator instead: