|
|
It's prob easier just to insert my code as it isn't very long..
class Main
{
function void main()
{
var Mandel Area;
let Area = Mandel.new();
do Area.DrawMandelbrot();
do Sys.wait(5000);
return;
}
} class Mandel
{
static int PIX;
constructor Mandel new()
{
let PIX = 2;
return this;
}
method void DrawMandelbrot()
{
var int X, Y, colour;
let X = 0;
let Y = 0;
let colour = 0;
while (Y < 10)
{
while(X < 20)
{
do Draw(X, Y, colour);
let X = X + PIX;
}
let X = 0;
let Y = Y + PIX;
if (colour<6) { let colour = colour+1;}
else {let colour = 0;}
}
return;
}
Method void Draw(int x,int y int col)
// draws pixels different colors like and old fashioned newspaper (theres 5 shades of grey)
return
}
There's no compilation errors, nothing comes up on the screen just ERR 5. Of course there's gdb and stack overflow in the real world Any help much appreciated, I'm off to do the washing up, well stuck !
|
Administrator
|
The problem (I'm pretty sure) is that your Mandel object requires zero bytes since there are no field variables declared. So you are asking the Memory.alloc() function to allocate a block containing zero bytes when you call the Mandel.new() constructor.
Since the whole idea for instantiating an object is for that object to carry it's own data unique to that object, an object that has no data is not very useful.
To verify that this is the problem, just make your PIX variable a field variable instead of a static variable and see if that makes the problem go away.
|
|
Hooragh, that thought did wander through my head earlier today..(in an incoherent way)
Just to make sure that I've understood Jack correctly;
It Isn't an imperative language At All (which is why all the OS functions are within objects eg Keyboard.readChar, Sys.halt(), Memory.peek()), therefore if I just want to draw some patterns on the screen I still have to create an object to do that (if I don't want to do it all from Main.main).
So if I don't want to manipulate any member variables at all, I still have to put some dummy one's in to keep the memory allocation happy? (The answer would appear to be yes)
Thank you !
|
Administrator
|
When you call Memory.peek() you are NOT using an object and this is not object oriented programming. You are merely calling the peek() function and telling the compiler that it is located within the Memory library. Memory is just a library, not an object.
The best solution in your case is to change your methods to functions and get rid of your constructor altogether -- if your object has no data members, then there isn't any point to having an object in the first place.
|
|
Ah yes..
You're right of course I should have said 'class' not object ie
which is why all the OS functions are within objects eg Keyboard.readChar should be which is why all the OS functions are within classes eg Keyboard.readChar
in my defence, and quoting the book:
9.2.7 The Jack Standard Library
The Jack language comes with a collection of built-in classes that extend the language’s capabilities. This
standard library, which can also be viewed as a basic operating system, must be provided in every Jack
language implementation. The standard library includes the following classes, all implemented in Jack:
• Math: provides basic mathematical operations;
• String: implements the String type and string-related operations;
• Array: implements the Array type and array-related operations;
• Output: handles text output to the screen;
• Screen etc..
Math This class enables various mathematical operations.
• function void init (): for internal use only.
• function int abs (int x): returns the absolute value of x.
(The bold is my emphasis)
Also Figure 9.5 Jack syntactic elements defines the "." (dot operator) as class membership therefore peek() is a method of class Memory no ?
If I've made a mistake in my understanding of what's going on, I'm happy to know...
Cheers
|
Administrator
|
Lozminda wrote
Ah yes..
You're right of course I should have said 'class' not object ie
Yes, all functions are defined within a class, but that doesn't make them or their use object-oriented. They are still intrinsically procedural and there's no need to instantiate an object to use them.
|
|
Sorry this is turning out to be a long one..
I thought I'd give your idea a go. At first (whilst doing some research) I couldn't find any Jack programs online or with the book that didn't define a constructor for the class/library, even if it was just a collection of functions. However I did find one and thought I'd use it as a template to follow your suggestion. (Because I agree, if one isn't using an object, then why create one?) So i tried:
class Main
{
function void main()
{
do Mandel.DrawMandelbrot();
return;
}
}
class Mandel
{
method void DrawMandelbrot()
{
//As before, I do call Draw in this function X,Y,color have bee defined etc
do Mandel.Draw(X,Y, color);
return;
}
method void Draw(int x, int y, int col)
{
// As before
return;
}
}
(Obvs I've removed a load of code so it's a bit less like War and Peace)
And I get the following error:
In Mandel.jack (line 15): In subroutine DrawMandelbrot: Method Mandel.Draw called as a function/constructor
In Main.jack (line 7): In subroutine main: Method Mandel.DrawMandelbrot called as a function/constructor
I haven't tried compiling the program i found on github which I used as a template (and it does make sense to me the 'library' definition, it just doesn't seem to work), I guess I should..
I'm also having trouble with static too, but that's for another post.
Again muchisimas gracias..
|
Administrator
|
But you are still defining a method. Methods can ONLY be called in association with an object. If you aren't going to have any objects of that class, then you can't call any methods. Make them functions.
Of the eight Operating System libraries, only one of them, String, has a constructor. Consequently, only String has any methods. Not even Array has a constructor.
|
|
Yes of course, it suddenly came to me method should be function:
class Mandel
{
function void DrawMandelbrot()
{
//As before, I do call Draw in this function X,Y,color have bee defined etc
do Mandel.Draw(X,Y, color);
return;
}
}
You beat me too it !
It's funny how sometimes I (and am sure others, having read about 10000 stack overflow posts) can't see the completely obvious when it's staring me in the face. I've been doing other stuff, and as soon as I sat down to my poota it came to me. Totally obvious. Jeesh !
Thanks very much for your help and patience !
All the best
|
|