Tran Nguyen
Tran Nguyen

Reputation: 35

Error: No matching overloaded function found

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

Upvotes: 1

Views: 16822

Answers (1)

Arnav Borborah
Arnav Borborah

Reputation: 11779

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

This is saying that the function accepts any type T as its argument. If one of the parameters of the function was a variable of type T, then the compiler would be able to find and deduce a specific overload.

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

Your stack is declared as:

Stack<char> *charStackPtr

Since the stack is always going to be of type char, you don't need to template argument T for it. The solution would be to remove it. Using a template variable in a function where the variable has a known type doesn't require the function to be a template itself.

Upvotes: 5

Related Questions