2009-12-07 78 views
-1

我有一个方法,产生一个int期望,但发现布尔错误,但当我切换到一个布尔值它说相同的错误,但反向int和布尔值。这里是我的代码:INT和布尔错误

private void compileDeclaration(boolean isGlobal) { 
     if (equals(theToken, "int")) { 
      accept("int"); 
      String ident = theToken; 
      if (!isIdent(theToken)) t.error("expected identifier, got " + theToken); 
      else if (isGlobal){ 
       symTable.allocVar(ident, isGlobal); 
      } 

      if (!isGlobal) cs.emit(Machine.ALLOC, symTable.stackFrameSize()); 
      //dprint("declaring int " + ident); 
      theToken = t.token(); 
      accept (";"); 
     } else if (equals (theToken, "final")) { 
      accept("final"); 
      accept("int"); 
      String ident = theToken; 
      if (!isIdent(theToken)) t.error("expected identifier, got " + theToken); 
      theToken = t.token(); 
      accept("="); 
      int numvalue = new Integer(theToken).intValue(); 
      if (!isNumber(theToken)) t.error("expected number, got " + theToken); 
      else if (numvalue = 0) { **//This is where it highlights my error** 
       symTable.allocConst(ident, numvalue); 
      } 

任何帮助将不胜感激。

+0

语言是什么呢? – ysth 2009-12-07 03:07:57

+0

不错,你展示的代码,你只需要显示错误信息,这将更容易发现。我认为菲利普·甘有正确的答案,你应该让他接受。 – OscarRyz 2009-12-07 03:30:34

回答

8

线

else if (numvalue = 0) { **//This is where it highlights my error** 

缺少等号,即

else if (numvalue == 0) { **//This is where it highlights my error** 
+0

并解释问题:“numvalue = 0”要求numvalue是一个int(或long),以便可以将0赋值给它,因此“int expected”。然而,if语句需要一个布尔表达式,并且赋值不是布尔表达式,因此“bool expected” - 两个不同的错误,由一个missing =引起。如果这是C/C++,那么你将会有很多乐趣:-) – 2009-12-07 03:20:28

+0

我同意C++会给你一个很好的运行!感谢您的解释 – 2009-12-07 06:14:58

1

很可能你在两个不同的地方调用它,一次是用整数和一次用布尔值。

无论是那个还是symTable.allocVar()都需要一个int。