c++ map/set iterator 不可解引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ map/set iterator not dereferencable
提问by berndh
I want to ask you for a hint, as I am a beginner and couldn't find any suitable answer in the internet. I am getting this error: debug assertion failed - map/set iterator not dereferencableat the line that looks like this:
我想问你一个提示,因为我是初学者,在互联网上找不到任何合适的答案。我收到此错误: 调试断言失败 - 映射/设置迭代器在如下所示的行中不可解引用:
pointA = active->pointNext(timeNext);
with the function pointNext()as I see everything is ok, and what concerns active, I have:
使用函数pointNext() ,因为我看到一切正常,以及与active 相关的问题,我有:
active = setS.data.end();
Some more info:
更多信息:
activeis multiset< classA, classB::classC >::const_iterator
active是multiset< classA, classB::classC >::const_iterator
setShas: setS.Q, setS.W, setS.Tand setS.data, whereby setS.datahas inside 0 in square braces. When I have multiset iterator declaration in .cpp file, during debug I cannot enter to see what is inside active, when it is in .h file, I can.
setS有:setS.Q, setS.W, setS.T和setS.data,其中setS.data在方括号内为 0。当我在 .cpp 文件中有 multiset 迭代器声明时,在调试期间我无法进入以查看内部活动的内容,当它在 .h 文件中时,我可以。
Having in .cpp I cannot enter active, so can imagine it's like pointer(iterator) cannot dereference, because is wrong inside. What if it is empty, i.e. if setS.datais empty? or if there's some trash inside?
在 .cpp 中我无法进入active,所以可以想象它就像指针(迭代器)不能取消引用,因为里面是错误的。如果它是空的,即如果setS.data是空的呢?或者里面是否有垃圾?
I know the thing was running under linux previously, is there some feature which I have to change for running on windows maybe? For example to change a number of template parameters to one only? (to properly ascribe setS.datato active, because I am not sure - do I do it properly?
我知道这个东西以前是在 linux 下运行的,是否有一些我必须更改才能在 Windows 上运行的功能?例如,将多个模板参数更改为仅一个?(正确地将setS.data归因于active,因为我不确定 - 我做得对吗?
Sorry for this rather chaotic post, I wanted to add my guesses for someone to neglect them if they are wrong. If something here is unclear or lack of some information, I will gladly add it. Can you please tell me what reasons could cause the dereferencablility error I get and where should I look for it? Because I am stuck and don't know how to proceed.
很抱歉这篇相当混乱的帖子,我想添加我的猜测,如果他们错了,请忽略他们。如果这里有什么不清楚或缺乏一些信息,我很乐意补充。你能告诉我什么原因会导致我得到的解除引用错误,我应该在哪里寻找它?因为我卡住了,不知道如何继续。
any help very appreciated, thanks!
非常感谢任何帮助,谢谢!
回答by NPE
Quite simply, since active
points to the container's end()
, you are not allowed to dereference it.
很简单,由于active
指向容器的end()
,您不能取消引用它。
I know the thing was running under linux previously
我知道这个东西以前是在 linux 下运行的
If the code was exactly like this and was "running", all this means that the error has never manifested itself in a manner that you've noticed.
如果代码完全像这样并且正在“运行”,则所有这一切都意味着错误从未以您注意到的方式表现出来。
回答by Martin York
This is your problem:
这是你的问题:
active = setS.data.end();
This returns an iterator to one passed the end of the container.
Thus the item that it is pointing at is not valid. You can not call any methods on the object that the iterator is referring too.
这将返回一个迭代器,指向通过容器末尾的迭代器。
因此它指向的项目无效。您也不能在迭代器所引用的对象上调用任何方法。
If you had done:
如果你做过:
active = setS.data.end();
if (setS.data.begin() != active)
{
// make sure the set is not empty first
--active;
active->methodCall(); // This would be OK
}
回答by juanchopanza
You cannot de-rederence the iterator returned by a standard library's end()
function, as this is "one past the last element". Typically you would iterate over the valid range, i.e. stopping beforeyou reach end()
:
您不能取消引用标准库end()
函数返回的迭代器,因为这是“经过最后一个元素的一个”。通常,您会遍历有效范围,即在到达之前停止end()
:
for(someIteratorType it = setS.data.begin(); it != setS.data.end(); ++it)
{
it->someMethod();
}
Or, in C++11,
或者,在 C++11 中,
for (const auto& elem : setS.data)
{
elem.someMethod();
}
回答by Ryan Guthrie
end() points to the element, after the last element. so end() isn't dereferencable.
end() 指向最后一个元素之后的元素。所以 end() 是不可解引用的。
You need to add a check to see if you're at the end, and if you are, don't dereference it.
您需要添加一个检查以查看您是否在最后,如果是,请不要取消引用它。
回答by Dinaiz
pointA = active->pointNext(timeNext);
tries must dereference "active" to call operator->(...) on it, but active is equal to setS.data.end();
尝试必须取消引用“active”才能调用 operator->(...) ,但 active 等于 setS.data.end();
end() returns an iterator to the element afterthe end of the container. Therefore, you can't dereference it.
end() 返回容器结束后元素的迭代器。因此,您不能取消引用它。