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 › "this" does not work
Page Index Toggle Pages: 1
"this" does not work (Read 475 times)
"this" does not work
Sep 27th, 2007, 9:48pm
 
Hi,

For a processing sketch I'm using the OBJ Loader library from SAITO and Polymonkey, however the question that I have relates to other libraries (usages of "this") as well:

Here's a snippet of my code:

class Automobile {

 OBJModel model;

 Automobile(String file) {
   model = new OBJModel(this);
   model.load(file);
 }

}

I get the following error message:

Semantic Error: No applicable overload was found for a constructor with signature "OBJModel(Temporary_4535_1358.Automobile)" in type "saito.objloader.OBJModel". Perhaps you wanted the overloaded version "OBJModel(processing.core.PApplet $1);" instead?

I think it has something to do with the class Automobile that changes the reference of "this", but how to solve this I don't know..

?BEnm


Re: "this" does not work
Reply #1 - Sep 27th, 2007, 11:10pm
 
To understand this, you need to realize that behind the scenes, all the classes you make are actually inner classes of the PApplet class.  "this" refers to the innermost class, so what you need to do is have a PApplet variable in the Automobile class that stores the containing class.  You'd then init the Automobile class using a constructor with an argument for the containing PApplet:
Code:

class Automobile{

OBJModel model;

PApplet parent;

Automobile(String file, PApplet _parent){
model = new OBJModel(_parent);
parent = _parent;
model.load(file);
}

}


You'd then create the Automobile like so:

Automobile myAuto = new Automobile(myString, this);

(from outside the Automobile class, so that "this" refers to the PApplet)  That should get you what you need, I think.
Re: "this" does not work
Reply #2 - Sep 27th, 2007, 11:34pm
 
ewjordan,

The whole PApplet story is a bit hard to self-construct for 'novice' p5 enthusiasts..
Your explanation, as well as your example code, get me where I need to be.

Thanks!

/BEnm


Page Index Toggle Pages: 1