Hello all,

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:

//The postfix parameter is a postfix string that was done through a shunting yard algorithm and is now passed through to the evaluate function
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.

conversion from 'std::stack<int>*' to non-scalar type 'std::stack<int>' requested

stack<int> resultStack = new stack<int>();

The bit on the right gives you a pointer to a stack<int>. You're trying to store that pointer value as a stack<int> object. This makes no sense. A stack<int>* is not the same as a stack<int>

invalid use of void expression

What kind of object does resultStack.pop() return? Does it return void (i.e. no returned object)?

cannot convert 'std::stack<int>' to 'int' in return

The function claims it returns an int( int Expression::evaluate(string postfix) ). You're trying to return a stack<int>. A stack<int> is not an int.

Also, you are popping a value off of the result stack BEFORE you pushed anything onto it. This is the cause of the error there. There is nothing to return to you!

Member Avatar for iamthwee

The other thing is postfix[i] will call a string of only one character long, whereas sqrt is four characters long. So you need to rethink that part at least.

Member Avatar for iamthwee

Just to add to this thread I did a bit of digging, and I think a handy way to handle sqrt and abs functions is to use an arbitary single digit character instead.

Otherwise you would have to tokenise your expression first.

http://programmingnotes.freeweq.com/?p=1436

^^In the above snippet the OP uses special characters to denote sqrt and modulus functions, it's definitely something you can take a look at to at least get some more ideas to move forward with your own code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.