Search code examples
c++stackexpressionpostfix-notationevaluate

Receiving stack errors when trying to evaluate a postfix expression in c++


I am getting 3 errors when I try to use stacks when I want to evaluate a postfix expression. I am not very experienced with the usage of stacks so please be patient with me.

Here is my code:

int Expression::evaluate(string postfix)
{
    // ERROR 1 //
    stack<int> resultStack = new stack<int>();

    int length = postfix.length();

    for (int i = 0; i < length; i++)
    {
        if ((postfix[i] == '+') || (postfix[i] == '-') || (postfix[i] == '*') || (postfix[i] == '/') || (postfix[i] == '^') || (postfix[i] == 'sqrt') || (postfix[i] == 'log') || (postfix[i] == 'abs') || (postfix[i] == '~'))
        {
            // ERROR 2 //
            int result = doTheOperation(resultStack.pop(), resultStack.pop(), postfix[i]);

            resultStack.push(result);
        }
        else if ((postfix[i] >= '0') || (postfix[i] <= '9'))
        {
            resultStack.push((int)(postfix[i] - '0'));
        }
        else
        {
        }
    }

    // ERROR 3 //
    return resultStack;
}

//The operations that must be done if a specific operator is found in the string
int Expression::doTheOperation(int left, int right, char op)
{
    switch (op)
    {
        case '+':
            return left + right;
        case '-':
            return left - right;
        case '*':
            return left * right;
        case '/':
            return left / right;
        case '^':
            return pow(left,right);
        case 'sqrt':
            if(right < 0)
            {
                string temp = "Square root of a negative number.";
                throw temp;
            }
            else
            {
                return (sqrt(right)) ;
            }
        case 'log':
            if (right < 0)
            {
                string temp = "Error. Not able to get the log of zero.";
                throw temp;
            }
            else
            {
                int temp = log10(right);
                return ceil(temp);
            }
        case 'abs':
            if (right < 0)
            {
                return (right*-1);
            }
            else
            {
                return right;
            }
        case '~':
            return (right*-1);

        default:
            return -1;
    }
    return -1;
}

Then it gives me the following errors:

error 1: conversion from 'std::stack<int>*' to non-scalar type 'std::stack<int>' requested
error 2: invalid use of void expression
error 3: cannot convert 'std::stack<int>' to 'int' in return

I will mark in the code where exactly these errors are occuring. I have absolutely no idea why I am getting these errors.


Solution

  • Error 1:

    The operator new returns a pointer to a dynamically allocated object (here std::stack<int> *) in the free store, but you just want to create a stack as a local variable (std::stack<int>).

    Change the line to:

    stack<int> resultStack;
    

    Error 2:

    You call resultstack.pop(), certainly expecting that it returns an int and pops it from the stack. unfortunately, pop() is void. It returns nothing, so you can't pass this result as a parameter.

    Even if it would return an int, you would have a hidden error: you have no garantee about the order of evaluation of parameters in a function call. So you do not know for sure wich of the two pops is done first.

    Change the line to:

        int p1 = resultStack.top(); resultStack.pop();
        int p2 = resultStack.top(); resultStack.pop();
        int result = doTheOperation(p1, p2, postfix[i]);
    

    Error 3:

    Your function is defined as returning an int. But you try to return the whole resultStack, which is a stack.

    If you want to return just the last value remainint on top of the stack, change the line to:

      return resultStack.top()