Templates 使用重载istream运算符将中缀转换为后缀
我的老师给了我的班级一个程序,它可以将像a+B*C这样的中缀表达式转换成后缀。我已经完成了其他代码,但是重载的istream操作符给了我一些问题。这就是我得到的错误: 错误:“convertToPostfix”没有依赖于模板参数的参数,因此“convertToPostfix”的声明必须可用[-fpermissive]| 以下是istream运算符的代码:Templates 使用重载istream运算符将中缀转换为后缀,templates,operator-overloading,postfix-notation,infix-notation,Templates,Operator Overloading,Postfix Notation,Infix Notation,我的老师给了我的班级一个程序,它可以将像a+B*C这样的中缀表达式转换成后缀。我已经完成了其他代码,但是重载的istream操作符给了我一些问题。这就是我得到的错误: 错误:“convertToPostfix”没有依赖于模板参数的参数,因此“convertToPostfix”的声明必须可用[-fpermissive]| 以下是istream运算符的代码: template <class U> std::istream& operator>>(std::istrea
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;
}
模板
std::istream&operator>>(std::istream&in、expression&e){
字符c;
e、 ifix=“”;
e、 pfix=“”;
做{
在>>e;
e、 pfix+=c;
}而(c!='.| | c!=');;
convertToPostfix();
返回;
}
我的老师告诉我们把convertToPostfix放在return语句之前,以便它同时返回中缀和后缀表达式。我正在寻找代码正确性作为反馈。提前感谢您提供的任何帮助。主要错误如下: 但除此之外,您还忘记分配给
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
有一个未定义的值(即它包含随机垃圾)。除非您指定了某个值,否则以任何方式使用它都是非法的。