简体   繁体   English

c ++ map / set iterator not dereferencable

[英]c++ map/set iterator not dereferencable

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 dereferencable at the line that looks like this: 我收到此错误: debug assertion failed - map / set iterator not dereferencable在看起来像这样的行:

pointA = active->pointNext(timeNext);

with the function pointNext() as I see everything is ok, and what concerns active , I have: 使用函数pointNext(),因为我看到一切正常,有什么问题是活跃的 ,我有:

active = setS.data.end();

Some more info: 更多信息:

active is multiset< classA, classB::classC >::const_iterator activemultiset <classA,classB :: classC> :: const_iterator

setS has: setS.Q, setS.W, setS.T and setS.data , whereby setS.data has inside 0 in square braces. setS有: setS.Q,setS.W,setS.TsetS.data ,其中setS.data在方括号内有0。 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. 当我在.cpp文件中有multiset迭代器声明时,在调试期间我无法进入以查看活动内部是什么,当它在.h文件中时,我可以。

Having in .cpp I cannot enter active , so can imagine it's like pointer(iterator) cannot dereference, because is wrong inside. 在.cpp中我无法进入活动状态 ,因此可以想象它就像指针(迭代器)不能解除引用,因为内部是错误的。 What if it is empty, ie if setS.data is empty? 如果它是空的,即如果setS.data为空,该怎么办? or if there's some trash inside? 或者里面有垃圾吗?

I know the thing was running under linux previously, is there some feature which I have to change for running on windows maybe? 我知道之前在linux下运行的东西,是否有一些我必须改变才能在Windows上运行的功能? For example to change a number of template parameters to one only? 例如,将一些模板参数更改为仅一个? (to properly ascribe setS.data to active , because I am not sure - do I do it properly? (正确地将setS.data归活动 ,因为我不确定 - 我是否正确地做到了?

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! 任何帮助非常感谢,谢谢!

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. 如果代码完全像这样并且“正在运行”,那么这一切意味着错误从未以您注意到的方式表现出来。

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
}

You cannot de-rederence the iterator returned by a standard library's end() function, as this is "one past the last element". 你不能去除由标准库的end()函数返回的迭代器,因为这是“一个过去的最后一个元素”。 Typically you would iterate over the valid range, ie stopping before you reach 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();
}

end() points to the element, after the last element. end()指向最后一个元素之后的元素。 so end() isn't dereferencable. 所以end()不是dereferecable。

You need to add a check to see if you're at the end, and if you are, don't dereference it. 您需要添加一个检查,看看您是否在最后,如果您是,请不要取消引用它。

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 after the end of the container. end()在容器结束返回元素的迭代器。 Therefore, you can't dereference it. 因此,您无法取消引用它。

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

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