0

I am getting this error message can anyone help me out.

I am getting the error code 'return' cannot convert from 'clsStack' to 'T' relating the two functions listed below.

Code is as follows

Class

template <class T>
class clsStack //this is the stack class that is used to handle the functions and any data that needs passing between functions
{
private:
    clsStack* data;
    int iTop;
    int iSize;
    void resize();
    bool needToResize();
    string sExpresion;
public:
    void stack()
    {
        iSize = 5;
        iTop = 0;
        data = new clsStack[iSize];
    }
    void push(T item);
    T peek();
    T pop();
    bool isEmpty();
};

The two functions I think the issue is related to

template <class T>
T clsStack<T>::peek() //this is used to look at the last number is the array
{
    if (iTop <= 0)
        throw out_of_range("Attempted to peek an empty stack. \n");
    return data[iTop - 1];
}

template <class T>
T clsStack<T>::pop()
{
    if (iTop <= 0)
        throw out_of_range("Attempted to pop an empty stack. \n");
        iTop++;
        return data[iTop];
}

The main function where these functions are been called

int evaluate(string sExpresion)
{
    clsStack<int> iValues;
    clsStack<char> cOperators;

    int iValue = 0;
    int iPosition = 0;
    bool bResult = false;

    while (iPosition < sExpresion.length())
    {
        char cSpot = sExpresion[iPosition];
        if (isDigit(cSpot))
        {
            iValue = (iValue * 10) + (int)(cSpot - '0');
        }
        else if (isOperator(cSpot))
        {
            if (cSpot == '(')
            {
                cOperators.push(cSpot);
                iValue = 0;
            }
            if (cSpot == '-')
            {
                cOperators.push(cSpot);
                iValue = 0;
            }
            else if (iValues.isEmpty() && cOperators.peek() == '-')
            {
                int iPrevValue = iValues.pop();
                int iPrevOperator = cOperators.pop();
                iPrevValue = operation(iValue, iPrevValue, iPrevOperator);
                iValues.push(iPrevValue);
                cOperators.push(cSpot);
            }
            else if (iValues.isEmpty())
            {
                iValues.push(iValue);
                cOperators.push(cSpot);
                iValue = 0;
            }
            else if (cSpot == ')')
            {
                iValues.push(iValue);
                while (cOperators.peek() != '(')
                {
                    cSpot = cOperators.pop();
                    iValue = iValues.pop();
                    int iPrev = iValues.pop();
                    iValue = operation(iPrev, iValue, cSpot);
                    iValues.push(iValue);
                }
                cOperators.pop();
                iValues.pop();
            }
            else
            {
                int iPrevValue = iValues.pop();
                int iPrevOperator = cOperators.pop();
                iPrevValue = operation(iPrevValue, iValue, iPrevOperator);
                iValues.push(iPrevValue);
                cOperators.push(cSpot);
                iValue = 0;
            }
        }
        iPosition++;
    }

    while (!cOperators.isEmpty())
    {
        int iPrev = iValues.pop();
        char cSpot = cOperators.pop();
        iValue = operation(iPrev, iValue, cSpot);
        bResult = true; 
    }

    return (sExpresion, iValue, bResult);
}

Any help is much apricated.

I_Zzack
  • 1
  • 1
  • 1
    Pop quiz: what type do you think `data` is? What do you think this pointer is pointing to? – Sam Varshavchik Nov 02 '20 at 12:37
  • 1
    We need a [mre] but presumably `clsStack* data` should be `T* data`? Your class presumably needs to also follow [the rule of 3/5](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Alan Birtles Nov 02 '20 at 12:51

0 Answers0