【问题标题】:infix to postfix using overloaded istream operator使用重载 istream 运算符的中缀到后缀
【发布时间】:2014-03-23 02:16:02
【问题描述】:

我的老师给我的班级提供了一个程序,可以将像 A+B*C 这样的中缀表达式转换为后缀。我已经完成了其他代码,但是重载的 istream 运算符给了我一些问题。这是我得到的错误:

错误:“convertToPostfix”没有依赖于模板参数的参数,因此“convertToPostfix”的声明必须可用[-fpermissive]|

这是 istream 运算符的代码:

template <class U>
std::istream& operator>>(std::istream& in, expression<U>& e) {
    char c;
    e.ifix ="";
    e.pfix ="";
    do {
        in >> e;
        e.pfix += c;
    }while(c!='.' || c!=';');

    convertToPostfix();

    return in;
}

我的老师告诉我们将 convertToPostfix 放在 return 语句之前,以便它同时返回中缀和后缀表达式。我正在寻找代码正确性作为反馈。提前感谢您为我提供的任何帮助。

【问题讨论】:

    标签: templates operator-overloading postfix-notation infix-notation


    【解决方案1】:

    主要错误如下:

    There are no arguments that depend on a template parameter

    但是另外你忘记分配给c

    // Declared
    char c;
    
    // Used here
    e.pfix += c;               // What is the value of `c` here?
    
    // and here
    while(c!='.' || c!=';');   // What is the value of `c` here?
                               // If you don't explicitly assign it a value
                               // contains a random value.
    

    我怀疑您想将输入中的下一个无空格字符读入c

    还有这一行:

    in >> e;
    

    似乎是一个递归定义。这肯定看起来不正确。

    【讨论】:

    • 分配给c是什么意思?
    • 目前变量c 有一个未定义的值(即它包含随机垃圾)。除非您指定了某个值,否则以任何方式使用它都是非法的。
    猜你喜欢
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2020-01-14
    • 2016-03-29
    相关资源
    最近更新 更多