gpt4 book ai didi

c++ - 错误 : No matching overloaded function found

转载 作者:行者123 更新时间:2023-11-30 01:37:52 25 4
gpt4 key购买 nike

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

谁能帮我解决为什么我不能运行这个程序,我总是犯 3 个错误:

Error C2672 'InfixToPostfix': no matching overloaded function found

Error C2783 'std::string InfixToPostfix(std::string)': could not deduce template argument for 'T'

E0304 no instance of overloaded function "InfixToPostfix" matches the argument list

最佳答案

template <class T>
string InfixToPostfix(string& str)

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

i am trying to use the stack template that I created, not from the library

您的堆栈声明为:

Stack<char> *charStackPtr

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

关于c++ - 错误 : No matching overloaded function found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48916778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com