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;
}
Can someone help me why i cannot run the program, I keep make 3 errors:
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>if you are not usingTat all? - NathanOlivertemplate <class T>and recompile. - Richard CrittenTso why do you need your function to be a template? And how is that related to your stack? If you want to try your Stack <T> in main (for testing) will you try to makemaintemplate as well? - Slava