0

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.

*Edited 2 Years Ago by Joemeister*