Invalid user conversion from char error (converting postfix using stack)

80 views Asked by At

I'm currently working on a project to convert from postfix to infix using a stack implemented through a linked list. I'm trying to take in a line and then pushing each character onto a stack however I keep getting the error.

Error: invalid user-defined conversion from ‘char’ to ‘const stack_element& {aka const std::basic_string&}’

Here is my code:

#include "stack.h"
string convert(string expression)
{
    stack c;
    string post = " ";
    for (int i =0; i<expression.length(); i++)
    {
        c.push(expression[i]);
    }
}
int main()
{
    string expression;
    cout<<" Enter a Post Fix expression: ";
    getline(cin,expression);
    return 0;
}

and here is the push function written in another .cpp file

void stack::push(const stack_element & item)
{
    cout<<"Inside push \n";
    stack_node *p = new stack_node;
    p->data = item;
    p->next = s_top;
    s_top = p;
}
2

There are 2 answers

0
Ngo Thanh Nhan On BEST ANSWER

do not use const as parameter in your push function

void stack::push(stack_element & item)
0
mystic_coder On

Change void stack::push(const stack_element & item)

to void stack::push(cons char& item) , As in your case , you are trying to create stac of characters . If you want generic use templates.