使用std :: vector不匹配operator = [英] no match for operator= using a std::vector
问题描述
我有一个这样宣布的课程:
I've got a class declared like this:
class Level
{
private:
std::vector<mapObject::MapObject> features;
(...)
};
,在其成员函数之一中,我尝试遍历该向量,如下所示:
and in one of its member functions I try to iterate through that vector like this:
vector<mapObject::MapObject::iterator it;
for(it=features.begin(); it<features.end(); it++)
{
/* loop code */
}
这对我来说似乎很简单,但是g ++给了我这个错误:
This seems straightforward to me, but g++ gives me this error:
src / Level.cpp:402:错误:'it =((const yarl :: level :: Level *)this)-> yarl中'operator ='不匹配: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>>&&)
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: note: candidates are: __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> > >&)
有人知道为什么会这样吗?
Anyone know why this is happening?
推荐答案
我猜这部分错误描述了您的问题:
I'd guess that this part of the error describes your problem:
(const yarl::level::Level*)this
是在其中找到此代码的成员函数a const限定的成员函数?如果是这样,则需要使用 const_iterator
:
Is the member function in which this code is found a const-qualified member function? If so, you'll need to use a const_iterator
:
vector<mapObject::MapObject>::const_iterator it;
如果成员函数是const限定的,则只有const限定的<$ c $重载成员向量上的c> begin()和 end()
将可用,并且两者都返回 const_iterator
s。
If the member function is const-qualified, then only the const-qualified overloads of begin()
and end()
on the member vector will be available, and both of those return const_iterator
s.
这篇关于使用std :: vector不匹配operator =的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!