Infix To Postfix Error

For some reason, I'm getting an error message saying: "Debug assertion failed: Deque iterator not dereferencable. I've read that this error means that I'm popping something off an empty stack, but I don't know why this error is occurring.

This error is being triggered by lines 41-50


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 if(isOperator(token))
{

while(precedence(s.top())>=precedence(token) && !s.empty())
{
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;
}
Last edited on
It's hard to read your code as there is no indentation. Also, it would be helpful if you posted the code that called that function.

Additionally, you haven't posted precedence() either.
Topic archived. No new replies allowed.