首页 > 解决方案 > 编译器错误 C2440 'return' 无法从 'clsStack 转换'到'T'

问题描述

我收到此错误消息,任何人都可以帮助我。

我收到错误代码 'return' cannot convert from 'clsStack' to 'T' 关联下面列出的两个函数。

代码如下

班级

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();
};

我认为这个问题与这两个功能有关

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];
}

调用这些函数的主函数

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);
}

任何帮助都非常有用。

标签: c++type-conversion

解决方案


推荐阅读