简体   繁体   中英

Vector iterator

I have a vector like this

std::vector<Sprite*> mDrawings;
std::vector<Sprite*>::iterator it = mDrawings.begin();

This gives an error

Error: no suitable user-defined conversion from std::_Vector_iterator<std::_Vector_val<Sprite *, std::allocator<Sprite *>>> to std::_Vector_iterator<std::_Vector_val<Sprite *, std::allocator<Sprite *>>> exists

But if i do the following

typedef std::vector<Sprite*> list;
list mDrawings;
list::iterator it = mDrawings.begin();

Then it works ???.

UPDATE: Im sorry it seems like the error was generated because of errors not related to the current code. I just saw the IDE underline with red and i thought that was the reason my application would not compile.

Your pasted code doesn't actually show the error. However, here's another shot in the dark:

Is it possible that in your original case, mDrawings is a member variable of some class (just guessign from the m prefix) and that your real code is in a const method? If so, you'd be trying to assing a const_iterator (since that's what calling begin() on a const vector will yield) to an iterator , which isn't possible.

Try using

std::vector<Sprite *>::const_iterator it = mDrawings.begin();

does that work better?

I think you pasted the wrong code - I think perhaps you did std::vector<Sprite>::iterator it = mDrawings.begin(); without the *

EDIT: In response to updated question:

Always look at the first compiler error - Your code, as posted on ideone, lacks an #include which makes Sprite visible.

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