错误:找不到匹配的重载函数

Tra*_*yen 1 c++ error-handling templates class

int main()
{
    string str;
    cout << "Enter Infix Expression \n";
    cin >> str;
    cout << "infix:" << str << "\n";
    string postfix = InfixToPostfix(str); // **error cause here**
    cout << "postfix:  " << postfix << "\n\n";

    system("pause");
    return 0;
}

// Function to evaluate Postfix expression and return output
template <class T>
string InfixToPostfix(string& str)
{
    Stack<char> *charStackPtr;
    charStackPtr = new Stack<char>();

    string postfix = ""; // Initialize postfix as empty string.
    for (int i = 0; i< str.length(); i++) {
        // If character is operator, pop two elements from stack, perform operation and push the result back. 
        if (IsOperator(str[i]))
        {
            while (!charStackPtr.empty() && charStackPtr.top() != '(' && HasHigherPrecedence(charStackPtr.top(), str[i]))
            {
                postfix += charStackPtr.top();
                charStackPtr.pop();
            }
            charStackPtr.push(str[i]);
        }
        // Else if character is an operand
        else if (IsOperand(str[i]))
        {
            postfix += str[i];
        }

        else if (str[i] == '(')
        {
            charStackPtr.push(str[i]);
        }

        else if (str[i] == ')')
        {
            while (!charStackPtr.empty() && charStackPtr.top() != '(') {
                postfix += charStackPtr.top();
                charStackPtr.pop();
            }
            charStackPtr.pop();
        }
    }while (!charStackPtr.empty()) {
        postfix += charStackPtr.top();
        charStackPtr.pop();
    }

    delete charStackPtr;
    return postfix;
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我为什么我无法运行该程序,我一直在犯 3 个错误:

错误 C2672“InfixToPostfix”:找不到匹配的重载函数

错误 C2783“std::string InfixToPostfix(std::string)”:无法推导出“T”的模板参数

E0304 没有重载函数“InfixToPostfix”的实例与参数列表匹配

Arn*_*rah 5

template <class T>
string InfixToPostfix(string& str)
Run Code Online (Sandbox Code Playgroud)

这就是说该函数接受任何类型T作为其参数。如果函数的参数之一是类型为 的变量T,那么编译器将能够找到并推导出特定的重载。

我正在尝试使用我创建的堆栈模板,而不是来自库

您的堆栈声明为:

Stack<char> *charStackPtr
Run Code Online (Sandbox Code Playgroud)

由于堆栈始终是 type char,因此您不需要为其模板参数T。解决方案是将其删除。在变量具有已知类型的函数中使用模板变量不需要函数本身就是模板。