简体   繁体   English

出队迭代器不可取消

[英]Dequeue iterator not dereferencable

Through doing a little research online, I've found that this is error is caused by trying to pop something off an empty stack. 通过在线进行一些研究,我发现这是由于尝试从空堆栈中弹出某些内容引起的错误。 In my code, I thought I tried to make sure this exact thing wouldn't happen by storing the top of my stack in a temporary string called c , but for some reason the error keeps occurring. 在我的代码中,我认为我试图通过将堆栈的顶部存储在名为c的临时字符串中来确保不会发生此确切的事情,但是由于某种原因,该错误不断发生。 This is a project related to converting infix notation to postfix notation. 这是一个与将中缀符号转换为后缀符号有关的项目。 When I input 1+2+3 , I should get 12+3+ , but instead this error occurs. 当我输入1+2+3 ,我应该得到12+3+ ,但是会出现此错误。 The error occurs in the precedence while loop. 错误发生在优先while循环中。

stack <string> InfixToPostfix(char * expr)
{
  // YOUR CODE STARTS HERE! 
 char* token;
 stack<string> s;
 string postfix;
 stack<string> p;
                                     // use stack s to manipulate the infix      to postfix
 s.push("(");
  token = strtok (expr," "); // get the first token
  while(!s.empty())
  {
          if(token=="(")
        {
            s.push(token);
          }
         else if(token==")")
      {
            string c=s.top();
            s.pop();
             while(c != "(")
            {
            postfix += c + " ";
            c = s.top();
            s.pop();
        }
  }
  else if(isNumber(token))
  {
      postfix+=token;
  }
  else 
  {
      while(precedence(s.top())>=precedence(token))
      {
          //put on bottom if need be

           string c = s.top();
            s.pop();
            postfix += c;
            postfix += " ";
      }
      s.push(token);
  }

token = strtok(NULL, " "); // use blanks as delimiter
  if (token == NULL) // no more tokens, exit the loop
     break;
  }
  p.push(postfix);
  return p;
}

Inside the if statements within the main while loop, you have some calls to pop() without checking if the deque is empty for example 在主while循环内的if语句内,您对pop()进行了一些调用,例如不检查双端队列是否为空。

 while(c != "(") {
     postfix += c + " ";
     c = s.top();
     s.pop();
 }

This should probably be: 这可能应该是:

while(c != "(" && !s.empty()) {

}

And the same below 和下面一样

while(precedence(s.top())>=precedence(token))
{
      //put on bottom if need be
      string c = s.top();
      s.pop();
      postfix += c;
      postfix += " ";
 }
 s.push(token);

These are quite likely sources of invalid access. 这些很可能是无效访问的来源。 In general you should try to use gdb or valgrind --tool=memcheck which will show you the source of error. 通常,您应该尝试使用gdbvalgrind --tool=memcheck ,它们将向您显示错误源。 Or any other debugger really... 还是其他任何调试器...

Also if the token is a single character as I suspect it should be, make it a char not a char * . 另外,如果令牌是我想应该的单个字符,则将其设为char而不是char * Then in your token comparisons use token == ')' (note the single quotes) which compares two characters and not what you have now which compares a char pointer with a string literal... This will probably apply to all the data structures that you maintain as well which should become, eg deque<char> ... 然后在令牌比较中使用token == ')' (请注意单引号),它比较两个字符,而不是现在比较将char指针与字符串文字比较的字符...这可能适用于所有您还维护应成为的内容,例如deque<char> ...

Finally pick up any decent introductory C++ book and read it. 最后,阅读并阅读所有不错的C ++入门书。 You will thank yourself later. 稍后您将感谢您。

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

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