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 › Undefined constructor
Page Index Toggle Pages: 1
Undefined constructor? (Read 954 times)
Undefined constructor?
Feb 2nd, 2009, 2:14am
 
Hello everyone, I am new to Processing and have just been starting to play with it in the past couple of weeks. I am working on some small exercise to make myself familiar with I/O and bumped into a problem that I couldn't figure out.

I am creating a sketch that loads coordinate data (a pair of x, y) from a txt file. And then I want to save the coordinates into an object I defined. The problem is, when I tried to create a class with overloaded constructor, the problem always tells me the constructor is not defined. Anyone's help would be highly appreciated.

Below are the pde source file and the text file.

[load_text_file.pde]
-----------------------------------------------------------
Point[] points = new Point[5];

String[] s = loadStrings("points.txt");
for(int i=0; i<s.length; i++) {
 println(s[i]);
 points[i] = new Point(s[i]);
}

class Point {
 float _x;
 float _y;
 
 Point(float tempx, float tempy) {
   _x = tempx;
   _y = tempy;
 }
 
 Point(String tempPoints) {
   String[] coordinate = split(tempPoints, ',');
   _x = float(coordinate[0]);
   _y = float(coordinate[1]);
 }
}
------------------------------------------------------------

[points.txt]
------------------------------------------------------------
100,50
25,70
60,60
------------------------------------------------------------

The error message is:
The constructor Point(String) is undefined.



Thanks!
Re: Undefined constructor?
Reply #1 - Feb 2nd, 2009, 3:59am
 
Edit: scratched my first thought.

If you do not use the void setup() method, you need to declare your class before the code that uses it.
Re: Undefined constructor?
Reply #2 - Feb 2nd, 2009, 5:07am
 
Thank you very much for your help, NoahBuddy. I added in the 'setup' method and the problem is solved.
Page Index Toggle Pages: 1