简体   繁体   English

if语句中的“预期表达式”错误

[英]'expected expression' error within if statement

I'm having to learn c++ on the fly with my data structure class. 我必须通过我的数据结构类即时学习c ++。 I only previously have experience with java which is why my error feels trivial but I am struggling to figure it out. 我以前只有Java的经验,这就是为什么我的错误让人觉得微不足道,但是我正在努力弄清楚。 I am getting an 'expected expression' error and am unsure what it is telling me. 我收到“期望的表达式”错误,不确定它告诉我什么。 I have tried swapping out my literal expressions out for ASCii code values and that sort of thing. 我已经尝试将我的文字表达式换成ASCii代码值之类的东西。 The error occurs on all of my if statements. 该错误发生在我所有的if语句上。 I do have all of my needed '#include' statements. 我确实有所有需要的“ #include”语句。 Thanks for any help. 谢谢你的帮助。

stack<double> doublestack;
char *input = new char[255]();
char *token = new char[255]();
cout << "Enter a valid postfix expression" << endl;
cin.getline(input, 255);
token = strtok(input, " ");
while (token != NULL) {
    if (token* == '*')
    {
        double b = doublestack.top();
        double a = doublestack.top();
        doublestack.pop();
        doublestack.pop();
        doublestack.push(b * a);
    }
    else if (token* == '/')
    {
        double b = doublestack.top();
        double a = doublestack.top();
        doublestack.pop();
        doublestack.pop();
        doublestack.push(b / a);
    }
    else if (token* == '+')
    {
        double b = doublestack.top();
        double a = doublestack.top();
        doublestack.pop();
        doublestack.pop();
        doublestack.push(b + a);
    }
    else if (token* == '-')
    {
        double b = doublestack.top();
        double a = doublestack.top();
        doublestack.pop();
        doublestack.pop();
        doublestack.push(b - a);
    }
    else
    {
        doublestack.push(atof(token));
    }
    token = strtok(NULL, " ");
}

If it helps to know, I am getting a postfix expression and pushing the numbers into a stack. 如果有帮助,我将获得一个后缀表达式并将这些数字推入堆栈。 When I find an operational character, I will pop the top two numbers to perform the operation. 找到操作字符后,我将弹出前两个数字以执行操作。 Once the postfix is finished, I should be left with one number and that is the answer to the postfix expression. 后缀完成后,我应该留下一个数字,这就是后缀表达式的答案。

The error messages are as follows... 错误消息如下...

stack1.cpp:24:20: error: expected expression
    if (token* == '*')
               ^
stack1.cpp:32:25: error: expected expression
    else if (token* == '/')
                    ^
stack1.cpp:40:25: error: expected expression
    else if (token* == '+')
                    ^
stack1.cpp:48:25: error: expected expression
    else if (token* == '-')

In this case, a = b 在这种情况下,a = b

    double b = doublestack.top();
    double a = doublestack.top();
    doublestack.pop();
    doublestack.pop();

guess you u want get last value(top()), then remove it from stack(pop()), then get next last value and so on 猜猜你要获取最后一个值(top()),然后将其从堆栈中移除(pop()),然后获取下一个最后一个值,依此类推

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

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