Koen Demonie
Koen Demonie

Reputation: 539

Iterator on vector error for operands "=" and "!="

using namespace std;
    vector<IDrawable*>::const_iterator itDrawable;
    for(itDrawable= scene.getDrawables().begin(); itDrawable!=scene.getDrawables().end();itDrawable++){
        IDrawable *drawable =(*itDrawable);
        drawable->draw();
    }

This code is passing me the error:

Description Resource Path Location Type no match for 'operator!=' (operand types are 'std::vector<IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<const IDrawable* const*, std::vector<const IDrawable*> >}')

And

Description Resource Path Location Type no match for 'operator=' (operand types are 'std::vector<IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<IDrawable* const*, std::vector<IDrawable*> >}' and 'std::vector<const IDrawable*>::const_iterator {aka __gnu_cxx::__normal_iterator<const IDrawable* const*, std::vector<const IDrawable*> >}')

I have looked these up and i should have something to do with the const_iterator ? yet my scene.getDrawables() looks like:

const std::vector<const IDrawable*>& getDrawables() const {
        return drawables;
    }

So the iterator should be a const_iterator right ? I have no clue what has to change...

Upvotes: 1

Views: 1140

Answers (2)

Daniel Frey
Daniel Frey

Reputation: 56863

The formatting of your question hides the real problem, but you used:

vector<IDrawable*>::const_iterator itDrawable;

where you should have used

vector<const IDrawable*>::const_iterator iDrawable;

Likewise, you then need

const IDrawable *drawable =...

instead of

IDrawable *drawable =...

All this due to the fact that you have a std::vector<const IDrawable*>.

Upvotes: 2

vsoftco
vsoftco

Reputation: 56557

Your

const std::vector<const IDrawable*>& getDrawables() const

returns a const reference to a vector of const IDrawable* pointers. However your

vector<IDrawable*>::const_iterator itDrawable; 

declares a const_iterator to a vector of different type (IDrawable*, not const IDrawable*). Either change the definition to vector<const IDrawable*>::const_iterator itDrawable; or simply use auto to declare the iterator in your for loop,

for(auto itDrawable= scene.getDrawables().cbegin(); ...)

Upvotes: 2

Related Questions