简体   繁体   English

C ++不匹配“ operator =”(后缀到后缀)

[英]C++ no match for 'operator=' (postfix to infix)

I am writing this program that converts postfix to infix. 我正在编写将后缀转换为中缀的程序。 I manage to code a program that does the opposite but when I try to create this particular program I have encountered many issues that I can't really understand what I am doing wrong. 我设法编写了一个与之相反的程序,但是当我尝试创建此特定程序时,遇到了很多我无法真正理解自己做错了什么的问题。

The following is my program: I can't get it to compile. 以下是我的程序:无法编译。 in my mind, it is a simple problem but I just can't figure out the issue. 在我看来,这是一个简单的问题,但我无法弄清楚这个问题。

/*
 * crate function toInfix
 * create empty stack
 * initialize output, and operand1 and operand2 variables strings
 * for loop entired input
 * if input is operator
 * pop thwo operands from the stack
 * combine the two operands with the operator in between
 * push the new expression on the stack
 * if input is operand then push in the stack
 */

string toInfix(string input)
{
    stack <char> stk;
    string output, oprnd1, oprnd2;
    for(auto& in : input)
    {
        if(isSymbol(in))
        {
            oprnd1 = stk.pop();
            oprnd2 = stk.pop();
            output = oprnd1 + in + oprnd2;
            stk.push(output);
        }
        stk.push(in);
    }
    while(!stk.empty())
    {
        output = stk.pop();
    }
    return output;
}

Like Benjamin said, pop returns void. 就像本杰明所说的, pop返回无效。 Therefore, setting a string to void is not possible. 因此,无法将字符串设置为void。 To fix this, set it equal to the top element, and then pop it. 要解决此问题,请将其设置为与top元素相等,然后将其弹出。

output += stk.top();
stk.pop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM