简体   繁体   English

当队列不为空时,queue.front()上的“ deque迭代器不可解除”

[英]“deque iterator not dereferencable” at queue.front() when queue is not empty

I am sure this question gets asked a lot - in fact i found 10+ answers while googling the problem. 我敢肯定这个问题会被问到很多-实际上,我在搜寻问题时发现了10多个答案。 But all of them pointed out not to call queue.front() when the queue is empty. 但是他们都指出,当队列为空时,不要调用queue.front()。

I have the following code: 我有以下代码:

std::queue<Message*> MessageQ;    //declaring  the queue
//...
MessageQ.push(Msg);  //filling the Queue
//...
if (!MessageQ.empty())
{
    Message* ToReturn = MessageQ.front();
    MessageQ.pop();
    return ToReturn;
}
return 0;

and i get the "deque iterator not dereferencable" error at the .front() line. 而我在.front()行收到“无法取消deque迭代器”错误。

what other problems might trigger such an error when using queue.front()? 使用queue.front()时,还有哪些其他问题可能会触发此类错误?

I even tried commenting the front line out - the message gets popped like it should. 我什至尝试将第一行注释掉-消息像预期的那样弹出。

Thanks for your time. 谢谢你的时间。

I'm sorry, it was a dumb mistake on my part. 对不起,这是我的一个愚蠢的错误。 I had a memset() somewhere in the constructor, which overwrote the queue with the data of an empty stack, as i changed the type this afternoon. 我在构造函数的某处有一个memset(),当我今天下午更改类型时,该队列用空堆栈的数据覆盖了队列。

Lesson learned, and thanks for Your answers! 经验教训,并感谢您的答复!

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

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