STL 迁移问题(VS 2003 -> 2005)

发布于 2024-07-09 09:42:05 字数 732 浏览 4 评论 0原文

我刚刚将一个项目从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-07-16 09:42:05

您的程序不正确,因为 NULL 无法转换为迭代器。 我真的不知道你希望这些迭代器被初始化为什么。 如果您需要保证迭代器不在容器中但仍然“有效”,则可以使用默认构造函数:

typedef std::vector<CSomeObject*> myvector_t;
void SomeFn( myvector_t::iterator it,
             myvector_t::iterator itBegin = myvector_t::iterator(),
             myvector_t::iterator itEnd = myvector_t::iterator() );

但是请注意,如果您这样做,ititBeginitEnd 将无法以有意义的方式进行比较! 只有从给定容器获得的迭代器才有意义。 最后,我建议不要使用 itBeginitEnd 的默认值。 如果您确实不需要这些,请创建另一个不带参数的函数并做一些有意义的事情。 即:

typedef std::vector<CSomeObject*> myvector_t;
void SomeFn( myvector_t::iterator it,
             myvector_t::iterator itBegin,
             myvector_t::iterator itEnd );
void SomeFn( myvector_t::iterator it ); // No begin/end arguments

您的程序的另一个问题是使用向量来存储指针。 这实在是不安全。 确保在未先删除元素的情况下永远不会从向量中删除元素。 您可能还会遇到复制对象的算法的问题。 最好在向量中使用智能指针。

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:

typedef std::vector<CSomeObject*> myvector_t;
void SomeFn( myvector_t::iterator it,
             myvector_t::iterator itBegin = myvector_t::iterator(),
             myvector_t::iterator itEnd = myvector_t::iterator() );

Note, however, that if you do so, it, itBegin and itEnd 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 for itBegin and itEnd. If you really need to not have these, create another function without the arguments and do something meaningful. i.e.:

typedef std::vector<CSomeObject*> myvector_t;
void SomeFn( myvector_t::iterator it,
             myvector_t::iterator itBegin,
             myvector_t::iterator itEnd );
void SomeFn( myvector_t::iterator it ); // No begin/end arguments

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.

人生戏 2024-07-16 09:42:05

2003 年,std::vector::iterator 只是 T *。 在 2005 年,至少在调试模式下,它是一个类,因此您不能使用 NULL 作为其值(NULL 解析为 0 >)。

您可以使用默认构造的迭代器来代替:

std::vector<CSomeObject*>::iterator itBegin = std::vector<CSomeObject*>::iterator()

In 2003, std::vector<T>::iterator is just T *. In 2005, at least in debug mode, it is a class, and hence you can't use NULL for its value (NULL resolves to 0).

You can use a default-constructed iterator instead:

std::vector<CSomeObject*>::iterator itBegin = std::vector<CSomeObject*>::iterator()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文