no match for operator= using a std::vector

13,279

Solution 1

I'd guess that this part of the error describes your problem:

(const yarl::level::Level*)this

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;

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_iterators.

Solution 2

Did you close your right-angle bracket here?

vector<mapObject::MapObject::iterator it;

If you want a vector of objects, your object needs an operator=. Does MapObject have one? If not, consider a vector of pointers to MapObject.

Share:
13,279
Max
Author by

Max

Updated on June 04, 2022

Comments

  • Max
    Max almost 2 years

    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 */
    }
    

    This seems straightforward to me, but g++ gives me this error:

    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?