c++ - 不匹配 operator= 使用 std::vector

标签 c++ stl vector constants

我有一个这样声明的类:

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/

相关文章:

c++ - 对于输入迭代器,为什么 a == b 并不意味着++a ==++b?

c++ - 在 map 中搜索 : Member vs. 非成员 lower_bound

c++ - 在 vector 中保留插入元素的地址(C++)

vector - 在 Rust 中连接向量的最佳方法是什么?

c++ - 缓存友好性 std::list 与 std::vector

c++ - 如何创建采用 wstring 的派生类并调用采用字符串的基类构造函数?

c++ - WinCE中,CreateFile函数: File open failed

c++ - C++ 中的动态(类型)二进制缓冲区?

c++ - 以派生类作为参数的专用模板方法

c++ - 声明是否可以转义其封闭的 namespace ?