我有一个这样声明的类:
class Level
{
private:
std::vector<mapObject::MapObject> features;
(...)
};
在它的一个成员函数中,我尝试像这样遍历该 vector :
vector<mapObject::MapObject::iterator it;
for(it=features.begin(); it<features.end(); it++)
{
/* loop code */
}
这对我来说似乎很简单,但是 g++ 给我这个错误:
src/Level.cpp:402: error: no match for ‘operator=’ in ‘it = ((const yarl::level::Level*)this)->yarl::level::Level::features.std::vector<_Tp, _Alloc>::begin [with _Tp = yarl::mapObject::MapObject, _Alloc = <code>std::allocator<yarl::mapObject::MapObject>]()’</code><br/>
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: <code>__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,</code>std::vector > >& <code>__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,</code>std::vector > <code>>::operator=(const __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*, ``std::vector<yarl::mapObject::MapObject, std::allocator<yarl::mapObject::MapObject> > >&)</code>
有人知道为什么会这样吗?
最佳答案
我猜这部分错误描述了您的问题:
(const yarl::level::Level*)this
找到此代码的成员函数是否为 const 限定的成员函数?如果是这样,您将需要使用 const_iterator
:
vector<mapObject::MapObject>::const_iterator it;
如果成员函数是 const 限定的,则只有成员 vector 上的 begin()
和 end()
的 const 限定重载可用,并且两者都返回 const_iterator
。
关于c++ - 不匹配 operator= 使用 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3041673/