简体   繁体   中英

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?

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_iterator s.

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.

@James McNellis's answer (the "green checked" best answer) fixed my error with a similar description, however my error was caused by what @tmarthal mentioned his answer post (not defining an assignment operator). His suggested fix is to include an assignment operator but I just wanted to add that I was also able fix this error by using std::vector<>::const_iterator instead of std::vector<>::iterator for classes where no assignment operator is defined. I'm not sure whether this truly a correct fix or just something that kept the compiler from complaining however.

If I were you, I would check to see that the mapObject::MapObject has a default constructor and a public assignment operator.

Somewhere in the class header, you should see something like:

public:
    MapObject();
    operator=(const MapObject &mapObject);

Which means that the class has a default constructor and an assignment operator.

A std::vector cannot be instantiated using a class without a default constructor, and you cannot iterate through the class as above without an assignment operator.

So, add an assignment operator to your class definition and your iteration will compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM