سؤال

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:

pointA = active->pointNext(timeNext);

with the function pointNext() as I see everything is ok, and what concerns active, I have:

active = setS.data.end();

Some more info:

active is multiset< 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. 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.

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.data is empty? 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? 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?

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.

I know the thing was running under linux previously

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". Typically you would iterate over the valid range, i.e. stopping before you reach end():

for(someIteratorType it = setS.data.begin(); it != setS.data.end(); ++it)
{
  it->someMethod();
}

Or, in C++11,

for (const auto& elem : setS.data)
{
  elem.someMethod();
}

end() points to the element, after the last element. so end() isn't dereferencable.

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();

end() returns an iterator to the element after the end of the container. Therefore, you can't dereference it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top