c++ - 当队列不为空时,在queue.front()处为"deque iterator not dereferencable"

标签 c++ queue std

我确信这个问题被问了很多 - 事实上,我在谷歌搜索这个问题时找到了 10 多个答案。但他们都指出当队列为空时不要调用queue.front()。

我有以下代码:

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;

我在 .front() 行收到“deque iterator not dereferencable”错误。

使用queue.front()时还有哪些其他问题可能会触发此类错误?

我什至尝试评论第一行 - 消息会像它应该的那样弹出。

感谢您的宝贵时间。

最佳答案

抱歉,这是我犯的一个愚蠢的错误。我在构造函数中的某个地方有一个 memset() ,它用空堆栈的数据覆盖了队列,因为我今天下午更改了类型。

吸取教训,感谢您的回答!

关于c++ - 当队列不为空时,在queue.front()处为"deque iterator not dereferencable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23742839/

相关文章:

c - 我的错误是什么?

c++ - Libreoffice API(UNO):来自xTextField的文本和数据

c++ - C++ 编码人员通常会显式地进行 move ,还是只依赖编译器来完成?

c++ - std::ostream::operator<< 没有为 std::string 定义?

javascript - QUEUE and 'IF' 'ELSE' 'WHILE' statements from scratch and running functions

c - 如何在c中将特定条目移动到队列头?

c++ - 将 std::function<void(string, string)> 转换为通用 std::function<void()>

c++ - 从 C++ 遍历 Lua 表?

c++ - std::bind 一个成员函数到 nullptr 处的一个实例导致看似随机的 this 指针

c# - 用于处理 MySQL 的带有队列的线程