2009-12-07 77 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。