We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Hiding enclosing type
Page Index Toggle Pages: 1
Hiding enclosing type (Read 761 times)
Hiding enclosing type
Dec 29th, 2008, 5:13am
 
Hi,
I'm getting a strange error that says "The nested type Fish cannot hide an enclosing type". I take that to mean that I cannot declare one class inside of another, but I have done no such thing. I think I may just may have the syntax wrong (never used Java much). Here's the relevant code:

class Fish {
 
 float maxSpd, spd, slowSpd, maxTurn, nMaxTurn;
 Vector position;
 Manager manager;
 
 Fish(Manager pManager, Vector pPos) {
   manager = pManager;
   maxSpd = pMaxSpd;
   spd = pSpd;
   slowSpd = pSlowSpd;
   maxTurn = pMaxTurn;
   nMaxTurn = pNMaxTurn;
   position = new Vector(pPos.x, pPos.y, pPos.z);
   manager.fishArray.push(this);
 }
}

Also, just as an aside, how would I assign default variables to function parameters? Just doing func(float pParam1 = 1.0) doesn't seem to work. Thanks.
Re: Hiding enclosing type
Reply #1 - Dec 29th, 2008, 1:01pm
 
1) You can nest classes.
I think you cannot declare a nested class with same name as the parent class.
So my guess is that you named your sketch "Fish". It was safe in older versions of Processing, but no longer. I was bitten by something similar when I made, for example, a sketch named PImage to test the PImage class: I had a conflict I had hard time to figure out.
What you need to know is that behind the scene, Processing creates a full Java class whose name is the sketch name. So you might end having a Fish class inside a Fish class without knowing!

2) You cannot have default values for functions (actually methods) parameters in Java. Would be useful, but it is not possible. Actually, recent versions (1.5?) of Java allows variable number of parameters, but the syntax isn't supported in Processing.
And you still have to manually supply the missing values (ie. to add tests in code: "if missing, use default value").

Actually, there is a way, not super elegant, but working, using method overloading: you declare two functions, one without parameters, one with full parameters (and perhaps more methods...).
Code:
Stuff CreateStuff(float p1, float p2)
{
// Use p1 and p2
}
Stuff CreateStuff(float p1)
{
return CreateStuff(p1, 1.0 /* default value */);
}
Stuff CreateStuff()
{
return CreateStuff(0.1, 1.0); /* default values */
}
Re: Hiding enclosing type
Reply #2 - Dec 30th, 2008, 4:25am
 
That was it, I just changed the name of my containing folder and it works fine now. Thanks.
Page Index Toggle Pages: 1