views:

164

answers:

3

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?

+1  A: 

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.

dmazzoni
The error comes from assigning the iterator, not assigning an object in the vector. You might get this error if the invalid line were actually `vector<MapObject::iterator> it;`, which is valid but wrong. It should be `vector<MapObject>::iterator it;`.
Mike Seymour
Note that unless you explicitly declare a copy assignment operator, the compiler will provide one for you, so even if you don't see one in the class definition, it still has one.
James McNellis
+2  A: 

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.

James McNellis
This was the problem. Thanks for your help.
Max
A: 

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.

tmarthal
`std::vector` _can_ be instantiated for a type without a default constructor, you just lose a bit of the functionality (e.g., when you resize the vector, you have to provide an object to copy into the new slots).
James McNellis