Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 933
Max Avatar asked Jun 24 '26 09:06

Max


1 Answers

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.

like image 175
James McNellis Avatar answered Jun 26 '26 00:06

James McNellis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!