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 error 'undefined'
Page Index Toggle Pages: 1
Constructor error 'undefined' (Read 1628 times)
Constructor error 'undefined'
Feb 12th, 2009, 5:40am
 
I'm trying to write a program using a class for the first time. Any help would be appreciated, thanks!


This is the error that I am getting:

'the constructor fish(int,int) is undefined'


This is the line that is highlighted:

'f1 = new fish(250,250);'


Here is my code:

fish f1;

void setup ()
{
 size (700,700);
 noStroke();
 smooth ();
 f1 = new fish(250,250);
}

void draw ()
{
 background (0,126,246);
 f1.display();
 
 f1.move();
}

class fish
{
 float fx; //x position of fish
 float fy; //y position of fish


 fish (float ifx, float ify)
 {
   float fx = ifx; //x position of fish
   float fy = ify; //y position of fish
     }

 void move ()
 {
   float dx = fx - mouseX;
   float dy = fy - mouseY;
   float e = 0.05;
   //horizontal movement
   if (abs(dx) < 100)
   {
     fx += dx*e;
   }
   if (abs(dx) > 200)
   {
     fx -= dx*e;
   }
   //vertical movement
   if (abs(dy) < 100)
   {
     fx += dy*e;
   }
   if (abs(dy) > 200)
   {
     fx -= dy*e;
   }
 }

 void display ()
 {
   //draw fish
   //backeye
   fill (0);
   ellipse (fx+20,fy-20,20,25);

   //draw fins
   fill(220,105,12);
   //left fin
   triangle (fx-3,fy+30,fx+5,fy+68,fx+21,fy+28);
   //backfin
   triangle (fx-75,fy+14,fx-95,fy+35,fx-7,fy+12);


   //body
   fill (240,150,60);
   ellipse (fx,fy,60,75);

   //right fin
   fill(240,125,12);
   triangle (fx-20,fy+25,fx-10,fy+30,fx-35,fy+65);

   //draw fish lips
   fill(230,25,25);
   ellipse (fx+33,fy-3,12,30);
   fill(130,25,25);
   ellipse (fx+35,fy-3,6,15);

   //draw eye shadow
   fill (170,110,49);
   ellipse(fx+8,fy-13,20,25);

   //draw front eye
   fill(0);
   ellipse (fx+10,fy-15,20,25);

   //draw eye spots
   fill(255);
   //front
   ellipse (fx+10,fy-25,6,8);
   //back
   ellipse (fx+23,fy-30,4,6);
 }
}


Re: Constructor error 'undefined'
Reply #1 - Feb 12th, 2009, 6:01am
 
try
f1 = new fish(250f,250f);
Re: Constructor error 'undefined'
Reply #2 - Feb 12th, 2009, 6:06am
 
Thanks for replying. Smiley

I tried what you said, but the same error is popping up.

:/ I'm reading a basic tutorial on classes right now, I'll probably end up restructuring most of the program from scratch.
Re: Constructor error 'undefined'
Reply #3 - Feb 12th, 2009, 6:08am
 
This is the original program.

I'm trying to clean it up using classes and such for the fish and its movements.

//fish
float fx = 250;
float fy = 250;
float dx;
float dy;
float b;
void setup ()
{
 size (700,700);
 smooth ();
}

void draw ()
{
 noStroke();
 background (0,126,246);

 b=random (2,15);


 //draw fish
 //backeye
 fill (0);
 ellipse (fx+20,fy-20,20,25);

 //draw fins
 fill(220,105,12);
 //left fin
 triangle (fx-3,fy+30,fx+5,fy+68,fx+21,fy+28);
 //backfin
 triangle (fx-75,fy+14,fx-95,fy+35,fx-7,fy+12);


 //body
 fill (240,150,60);
 ellipse (fx,fy,60,75);

 //right fin
 fill(240,125,12);
 triangle (fx-20,fy+25,fx-10,fy+30,fx-35,fy+65);

 //draw fish lips
 fill(230,25,25);
 ellipse (fx+33,fy-3,12,30);
 fill(130,25,25);
 ellipse (fx+35,fy-3,6,15);

 //draw eye shadow
 fill (170,110,49);
 ellipse(fx+8,fy-13,20,25);

 //draw front eye
 fill(0);
 ellipse (fx+10,fy-15,20,25);

 //draw eye spots
 fill(255);
 //front
 ellipse (fx+10,fy-25,6,8);
 //back
 ellipse (fx+23,fy-30,4,6);
 dx = fx-mouseX;
 dy = fy-mouseY;
 //fx = fx+(random(-5,5));
 //fy = fy+(random(-5,5));
 if (keyPressed == false)
 {
   //how close
   if (abs(dx) < 60)
   {
     fx = fx+(dx/2);
     //bubbles
     stroke (255);
     fill (0,0,0,0);
     ellipse (random (fx+40,fx+60),random (fy-40,fy-60),b,b);
     ellipse (random (fx+65,fx+85),random (fy-65,fy-85),b,b);
   }
   //how distant
   if (abs(dx) > 300)
   {
     fx = fx-(dx/2);
   }

   //for verticle
   //how close
   if (abs(dy) < 60)
   {
     fy = fy+(dy/2);
     //bubbles
     stroke (255);
     fill (0,0,0,0);
     ellipse (random (fx+40,fx+60),random (fy-40,fy-60),b,b);
     ellipse (random (fx+65,fx+85),random (fy-65,fy-85),b,b);
   }

   if (abs(dy) > 300)
   {
     fy = fy-(dy/2);
   }
 }
}
Re: Constructor error 'undefined'
Reply #4 - Feb 12th, 2009, 6:11am
 
It's funny that I just copy and paste your code and it works...
Re: Constructor error 'undefined'
Reply #5 - Feb 12th, 2009, 6:14am
 
the first one or the second one?
Re: Constructor error 'undefined'
Reply #6 - Feb 12th, 2009, 6:15am
 
both
Re: Constructor error 'undefined'
Reply #7 - Feb 12th, 2009, 6:16am
 
haha, weird...

Thanks!
Page Index Toggle Pages: 1