简体   繁体   English

矢量迭代器

[英]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. 我只是看到IDE用红色下划线,我认为这是我的应用程序无法编译的原因。

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? 在您的原始情况下, mDrawings是否可能是某个类的成员变量(只是从m前缀猜测),而您的实际代码在const方法中? 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. 如果是这样,您将尝试将const_iterator (因为在const向量上调用begin()会产生这种情况) iterator ,这是不可能的。

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(); 我认为您粘贴了错误的代码-我想也许您做了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. 始终查看第一个编译器错误-如ideone上所示,您的代码缺少#include使Sprite可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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