Compilation Error in Code

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Compilation Error in Code

sarthak
I was trying to create a simple Bank Accounting system. I have the following code:
class BankAccount {
  field String Name;
  field int AccNo;
  field int Bal;
  constructor BankAccount new(String a, int b, int c){
    let a = Name;
    let b = AccNo;
    let c = Bal;
    return this;
  }
  method void AmountTransfer(BankAccount b, int amt){
    var int Balance;
    let Bal = Bal - amt;
    let Balance = b.Bal - amt;
    let b.Bal = Balance;
    return;
  }
}
But it is showing the following compilation error:
In BankAccount.jack (line 14): In subroutine AmountTransfer: Expected (
In BankAccount.jack (line 15): In subroutine AmountTransfer: Expected [ or =
What is the problem with the above code?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compilation Error in Code

cadet1620
Administrator
sarthak wrote
I was trying to create a simple Bank Accounting system. I have the following code:
...
    let Balance = b.Bal - amt;
    let b.Bal = Balance;
...
But it is showing the following compilation error:
In BankAccount.jack (line 14): In subroutine AmountTransfer: Expected (
In BankAccount.jack (line 15): In subroutine AmountTransfer: Expected [ or =
What is the problem with the above code?
The problem is that Jack does not allow access to member variables using '.'. You need to write methods to read and write member variables:

    let Balance = b.GetBalance() - amt;
    do b.SetBalance(Balance);

[BTW, your assignments are backwards in your constructor.]

--Mark
jrd
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compilation Error in Code

jrd
I have an additional compilation error that has been baffling me for hours.  Is there a possibly a bug in the jack compiler?

Here is the offending snippet from a class/file called "MasterMind.jack":

            ......

135      /** Gets a new PlayerGuess */
136     method Array getPlayerGuess() {
137
138           // Print Current Turn to screen
139          do Output.printString("Turn #");
140          do Output.printInt(currentTurn + 1);
141          do Output.printString(":");
142
143          var boolean guessCheck;
144
145          let guessCheck = false; // Initialize flag to check if player guess is of correct length…
146
147          // Player enters guess…
148          var String input;
149          var Array result;
150          let input = String.new(maxPegs); // maxPegs is defined earlier in the Class as a field variable
151          let result = Array.new(maxPegs); // maxPegs is defined earlier in the Class as a field variable

             ......

             }

* The compiler keeps returning the following error: "In MasterMind.jack (line 143): In subroutine getPlayerGuess: Expected statement(do, let, while, return or if)"

I've checked all the documentation and it seems that, in line 143, I've properly declared the method boolean variable guessCheck.  However, the compiler keeps on finding errors on this line and I don't believe I need another expected statement (as the compiler is stating).  This is also the only compiler error in my code.

(PS: This identical problem declaring a method boolean variable is also occurring in another place later in my code in a different method.  If I solve this, the 2nd instance of the problem will probably solve as well.)

Pls help!  What am I missing here?  Is there any incorrect syntax in line 143 or the surrounding code?

- JRD
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compilation Error in Code

cadet1620
Administrator
jrd wrote
I have an additional compilation error that has been baffling me for hours.  Is there a possibly a bug in the jack compiler?

Here is the offending snippet from a class/file called "MasterMind.jack":

            ......

135      /** Gets a new PlayerGuess */
136     method Array getPlayerGuess() {
137
138           // Print Current Turn to screen
139          do Output.printString("Turn #");
140          do Output.printInt(currentTurn + 1);
141          do Output.printString(":");
142
143          var boolean guessCheck;

"In MasterMind.jack (line 143): In subroutine getPlayerGuess: Expected statement(do, let, while, return or if)"
All var declarations must be at the top of the subroutine, before the first statement.

--Mark
jrd
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Compilation Error in Code

jrd
In reply to this post by jrd
Thank you.  This resolves all the issues.
Loading...