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 › constructor undefined; new to OOP
Page Index Toggle Pages: 1
constructor undefined; new to OOP (Read 753 times)
constructor undefined; new to OOP
Feb 9th, 2010, 8:11pm
 
Thanks in advance for your help! I'm getting 'The constructor sketch_feb09b.CountupTimer() is undefined' for this code. I've tried putting the object declaration in the setup() block with the same result. I'm sure it's something stupid. Thanks again!

   PFont fontA;
   CountupTimer CountupTimer1 = new CountupTimer();
   
void setup()
 {
   size(1280, 720);
   
   fontA = loadFont("HelveticaNeue-Bold-48.vlw");
   textFont(fontA, 32); // Set the font and its size (in units of pixels)
   background(0);
 }
 
void draw()
{
  //DISPLAY TIMER
   textAlign(CENTER);
   text(CountupTimer1.display(), width/2, height-10);
}

class CountupTimer {
 int milliseconds;
 int seconds;
 int minutes;
 int hours;
 str displayTime;

 // Arguments: Timer Start in Millis; show minutes True/False; show hours true/false (if minutes is false, hours defaults to false)
 CountupTimer(int timerStart, boolean showMinutes, boolean showHours) {
   timerStart = millis();
   showMinutes = true;
   showHours = false;
 }

 void display() {
   // calculate elapsed time in days, hours, minutes, seconds, and milliseconds
   milliseconds = millis() - timerStart;
   seconds = milliseconds / 1000;
   minutes = seconds / 60;
   hours = minutes / 60;
   days = hours / 24;
   
   // Now we need to subtract elapsed minutes from seconds, hours from minutes, etc,
   // so that we don't end up with silly things like "01:65:3912:39123421"
   milliseconds -= seconds * 1000;
   seconds -= minutes * 60;
   minutes -= hours * 60;
   hours -= days * 24;
   
   // Lets set up our string before we actually print it:
   if(showMinutes && showHours) displayTime = nf(hours,2,0)+":"+nf(minutes,2,0)+":";
   else if(showMinutes) displayTime = nf(minutes,2,0)+":";
   displayTime += nf(seconds,2,0)+":"+nf(milliseconds,3,0);
   
   return displayTime;
 }
}
Re: constructor undefined; new to OOP
Reply #1 - Feb 9th, 2010, 10:28pm
 
Processing is expecting a constructor with no arguments declared in the CountupTimer class to match the line: "CountupTimer CountupTimer1 = new CountupTimer();"

Changing "void display()" to "String display()" might also help. Wink

And more nitpicking... The constructor that accepts arguments (int timerStart, boolean showMinutes, boolean showHours) only changes the these argument values (local copies, see http://processing.org/learning/basics/variablescope.html).

Perhaps you wanted these variables added to the class itself? Like hours, minutes, etc.
Re: constructor undefined; new to OOP
Reply #2 - Feb 11th, 2010, 8:30am
 
Ahhh, much better. Thanks a ton!

Corrected code:

PFont fontA;
  CountupTimer CountupTimer1 = new CountupTimer(millis(),true,false);
 
void setup()
{
  size(1280, 720);
 
  fontA = loadFont("HelveticaNeue-Bold-48.vlw");
  textFont(fontA, 32); // Set the font and its size (in units of pixels)
}

void draw()
{
  background(0);
 //DISPLAY TIMER
  textAlign(CENTER);
  text(CountupTimer1.display(), width/2, height-10);
}

class CountupTimer {
int milliseconds;
int seconds;
int minutes;
int hours;
int days;
String displayTime;
boolean showMinutes;
boolean showHours;
int timerStart;

// Arguments: Timer Start in Millis; show minutes True/False; show hours true/false
CountupTimer(int tempStart, boolean tempMinutes, boolean tempHours) {
 timerStart = tempStart;
 showMinutes = tempMinutes;
 showHours = tempHours;
}

String display() {
  // calculate elapsed time in days, hours, minutes, seconds, and milliseconds
  milliseconds = millis() - timerStart;
  seconds = milliseconds / 1000;
  minutes = seconds / 60;
  hours = minutes / 60;
  days = hours / 24;
 
  // Now we need to subtract elapsed minutes from seconds, hours from minutes, etc,
  // so that we don't end up with silly things like "01:65:3912:39123421"
  milliseconds -= seconds * 1000;
  seconds -= minutes * 60;
  minutes -= hours * 60;
  hours -= days * 24;
 
  // Lets set up our string before we actually print it:
  if(showMinutes && showHours) displayTime = nf(hours,2,0)+":"+nf(minutes,2,0)+":";
  else if(showMinutes) displayTime = nf(minutes,2,0)+":";
  displayTime += nf(seconds,2,0)+":"+nf(milliseconds,3,0);
 
  return displayTime;
}
}
Page Index Toggle Pages: 1