我在后缀计算器中遇到此错误:integerOperand无法解析或不是字段。下面,我显示了主要代码以及IntegerOperand类文件中的代码。我怎样才能解决这个问题?我试图从IntegerOperand类调用add函数。
public class IntegerOperand implements CalculatorOperand<IntegerOperand> {
BigInteger value;
IntegerOperand (BigInteger value) {
this.value = value;
}
public IntegerOperand add (IntegerOperand that) {
return new IntegerOperand(this.value.add(that.value));
}
public IntegerOperand subtract (IntegerOperand that) {
return new IntegerOperand(this.value.subtract(that.value));
}
public IntegerOperand multiply (IntegerOperand that) {
return new IntegerOperand(this.value.multiply(that.value));
}
public String toString () {
return value.toString();
}
}
public void operation (OperationType operation) {
T t1;
T t2;
if(stack.isEmpty())
{
t2= stack.pop();
t1= stack.pop();
stack.push(t1.IntegerOperand.add(t2));
}
}
答案 0 :(得分:1)
主要问题是您没有正确调用该函数。
// You don't need the class name
//stack.push(t1.IntegerOperand.add(t2));
stack.push(t1.add(t2));
第二,检查堆栈是否为空,然后检查堆栈是否为pop
。但是您应该检查堆栈是否为 not 为空:if (!stack.isEmpty())
。但是,由于您随后要对pop
进行两次调用,因此您应该检查堆栈中是否至少有2个项目。
if (stack.size() >= 2) {
t2 = stack.pop();
t1 = stack.pop();
stack.push(t1.add(t2));
}