从'char'到'const string&amp;的用户定义转换无效{aka const std :: basic_string <char>&amp;}'[-fpermissive]

时间:2017-10-19 02:15:25

标签: c++ string types type-conversion

我是一名学生,我的课程分为三个部分,但最后一部分是我遇到麻烦的地方,我找不到办法解决它,它告诉我:

  

无效的用户定义从'char'转换为'const string&amp; {又名   const std :: basic_string&amp;}'[-fpermissive]    while(!st.empty()&amp;&amp; st.top()!='('&amp;&amp;   hasHigherPrecedence(st.top(),EXPR [I])){

我的代码:

string StacksAndQueues::convertInfixToPostfix(const string &expr) {
stack<char> st;
int n = expr.length();
string res = "";

for(int i = 0;i<n;i++){
    if(expr[i] == '0' || expr[i] == '1' || expr[i] == '2' || expr[i] == '3' || expr[i] == '4' || expr[i] == '5' || expr[i] == '6' || 
    expr[i] == '7' || expr[i] == '8' || expr[i] == '9'){
        res = res + expr[i];
    }else if(expr[i] == '+' || expr[i] == '-' || expr[i] == '*' || expr[i] == '/'){
        while(!st.empty() && st.top() != '(' && hasHigherPrecedence(st.top(),expr[i])){
            res = res + st.top();
            st.pop();
        }
        st.push(expr[i]);
    }
    else if (expr[i] == '(') 
    {
        st.push(expr[i]);
    }

    else if(expr[i] == ')') 
    {
        while(!st.empty() && st.top() !=  '(') {
            res = res + st.top();
            st.pop();
        }
        st.pop();
    }
}
while(!st.empty()) {
    res = res + st.top();
    st.pop();
}
return res;

}

bool StacksAndQueues::hasHigherPrecedence(const string &stackTop, const string &op) {
return !((stackTop == string("+") || stackTop == string("-")) &&
         (op == string("*")       || op == string("/")));

}

0 个答案:

没有答案
相关问题