Max*_*Max 2 c++ stl const vector
我有一个这样的类声明:
class Level
{
private:
std::vector<mapObject::MapObject> features;
(...)
};
Run Code Online (Sandbox Code Playgroud)
在其中一个成员函数中,我尝试迭代遍历该向量,如下所示:
vector<mapObject::MapObject::iterator it;
for(it=features.begin(); it<features.end(); it++)
{
/* loop code */
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很简单,但是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 = std::allocator<yarl::mapObject::MapObject>]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669:注意:候选人是:__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std :: vector >>&__gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*,std :: vector>>::operator=(const __gnu_cxx::__normal_iterator<yarl::mapObject::MapObject*, ``std::vector<yarl::mapObject::MapObject, std::allocator<yarl::mapObject::MapObject> > >&)
任何人都知道为什么会这样吗?
Jam*_*lis 15
我猜这部分错误描述了你的问题:
(const yarl::level::Level*)this
Run Code Online (Sandbox Code Playgroud)
成员函数是否在其中找到此代码的const限定成员函数?如果是这样,你需要使用const_iterator:
vector<mapObject::MapObject>::const_iterator it;
Run Code Online (Sandbox Code Playgroud)
如果成员函数是const限定,那么只有的const限定的过载begin()和end()对成员向量将是可用的,并且这两个回报const_iterator秒.