return type must be class type error message

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

return type must be class type error message

sendhelp
Hello,

So I am receiving this error whenever I compile my jack file and cant find where I am wrong. could you please help me?

error: in subroutine new: the return type of constructor must be of the class type

my code:

class DodgerGame{
   field Dodgder dodger;
   field int direction,obstacles;

   constructor DodgerGame new() {
      let direction = 0;
      let obstacles = 2;
      let dodger = Dodger.new(50, 256, 30);
      return this;
   }
}
Reply | Threaded
Open this post in threaded view
|

Re: return type must be class type error message

WBahn
Administrator
You've defined a constructor that takes no arguments and then within that constructor call that same constructor, but with three arguments. Even if it succeeds, you will have infinite recursion.
Reply | Threaded
Open this post in threaded view
|

Re: return type must be class type error message

ivant
sendhelp wrote
error: in subroutine new: the return type of constructor must be of the class type
I don't see anything wrong with that part of your code. You can send me the whole thing if you want me to take a look at it. Just click on my name and then on the send email link.

WBahn wrote
You've defined a constructor that takes no arguments and then within that constructor call that same constructor, but with three arguments. Even if it succeeds, you will have infinite recursion.
This is not correct. The current class is called DodgerGame, while the internal constructor is for Dodger.
Reply | Threaded
Open this post in threaded view
|

Re: return type must be class type error message

WBahn
Administrator
Ah, you are correct. I didn't real closely enough. Thanks for catching that.
Reply | Threaded
Open this post in threaded view
|

Re: return type must be class type error message

WBahn
Administrator
In reply to this post by sendhelp
Are there other .jack files in the directory you re compiling? I'm guessing there are. It's possible that the problem is the interplay between files (though that particularly error wouldn't imply that, but that might just mean that the compiler is getting confused).

Try compiling just this file by itself and see if you get the same error. If it compiles fine, then it is some kind of interplay. Add the other files to the folder one at a time and compile them all each time until the error appears. Then remove all except this one and the last one added and see if the error is still there. If so, look carefully at the other file to see if there are any naming mistakes.
Reply | Threaded
Open this post in threaded view
|

Re: return type must be class type error message

ashort
This post was updated on .
In reply to this post by sendhelp
The compiler error "the return type of constructor must be of the class type" is self-explanatory - but your constructor return type "DodgerGame" matches your class type "DodgerGame", so I'm a little puzzled. WBahn must be on to something with regard to other files...

I just tried compiling this in my compiler (as well as the built-in compiler) and it compiles with no errors, so long as the file name is "DodgerGame.jack" (otherwise you get another error: "The class name doesn't match the file name".

As an aside, your dodger field is declared with type "Dodgder" - a misspelling, but it still compiles because the compiler silently assumes the "Dodger" class exists in another file when it sees  let dodger = Dodger.new(50, 256, 30).  Also, it doesn't care that you are assigning a return value of type "Dodger" to a field declared of type "Dodgder" - because of lack of type checking here, by design.