Saying 'undefined' when it is defined
in
Programming Questions
•
5 months ago
Hey, I'm new to constructors...hence why I may have one or two questions :)
From the below image, the error states that I haven't defined Car(int, int, int, int, float, int).
However, under the 'Class' portion of my code, I have defined it as int, int, int, float, float, int. So I'm not sure why its interpreting one of those data types wrongly:
Car(int colorR,int colorG,int colorB, float xpos, float ypos,int x) { // A constructor.
colorR = 255;
colorG = 0;
colorB = 0;
xpos = width/3;
ypos = height/3;
xspeed = 1;
Could it possibly be because I have the same line after it? (Though I've changed the values of some variables in it, am I right in saying modules/constructors should never have identical arguments? (Highlighted in
bold below)
// Learning Processing
// Daniel Shiffman
//2. Check the Object Oriented Program for the Car.
//Improve the code such that you can change the color.
Car myCar, myCar1, myCar2; // Declare car object as a globle variable.
void setup() {
size(200,200);
// Initialize Car object
myCar = new Car(); // Initialize car object in setup()
//by calling constructor.
myCar1 = new Car(0,255,0,width/4,height*0.8,2);
myCar2 = new Car(0,0,255,width/8*5,height*0.5,3);
}
void draw() {
background(255);
// Operate Car object.
myCar.move(); // Operate the car object in draw( )
//by calling object methods using the
//dots syntax.
myCar.display();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myCar1.move();
myCar1.display();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myCar2.move();
myCar2.display();
}
class Car { // Define a class below the rest
//of the program.
float colorR, colorG, colorB, xpos, ypos, xspeed;
Car() { // A constructor.
colorR = 0;
colorG = 255;
colorB = 0;
xpos = width/2;
ypos = height/2;
xspeed = 1;
}
Car(int colorR,int colorG,int colorB, float xpos, float ypos,int x) { // A constructor.
colorR = 255;
colorG = 0;
colorB = 0;
xpos = width/3;
ypos = height/3;
xspeed = 1;
}
Car(int colorR,int colorG,int colorB, float xpos, float ypos,int x) { // A constructor.
colorR = 0;
colorG = 0;
colorB = 255;
xpos = width/3*2;
ypos = height/3*2;
xspeed = 1;
}
void display() { // Function.
// The car is just a square
rectMode(CENTER);
stroke(0);
fill(colorR,colorG,colorB);
//Body
rect(xpos,ypos,50,30);
//Wheel
ellipse(xpos-15,ypos+15,10,10);
ellipse(xpos+15,ypos+15,10,10);
}
void move() { // Function.
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
}
}
1