compiling error (constructor name)

Button1 button1;
Button2 button2;

float x = 0;
float h = 200;
float w = 400;
float e = 600;


void setup() {
  size(800, 800);
  background(255);
  stroke(0);
  fill(255);
  button1 = new Button1(0, 0, 400, 200);
}

void draw() {
}

class Button1 {
  int xpos;
  int ypos;
  int w;
  int h;

  Button(float tempXpos, float tempYpos, float tempWidth, float tempHeight) {
    xpos = tempXpos;
    ypos = tempypos;
    w = tempWidth;
    h = temHeight;
  }
  void display() {
    stroke(0);
    fill(255);
    rectMode(CORNER);
  }
  void rectangle() {
    rect(xpos, ypos, w, h);
    if (mousePressed==true) {
      if (mouseX>xpos&&mouseX<xpos+200&&mouseY>ypos&&mouseY<ypos+200) {
        fill(0);
      }
    }
  }
}
class Button2 {
}

the error reads "(the constructor in sketch+4in1.Button1(int,int,int,int) is undefined" I thought i converted all ints to floats... Can someone help me? thanks :D

Tagged:

Answers

  • edited June 2014 Answer ✓

    The problem was that the initialiser of the class needs to have the same name as the class. I also changed a few variable types and such as you made a decent amount of mistakes. Here is the code:

    Button1 button1;
    Button2 button2;
    
    float x = 0;
    float h = 200;
    float w = 400;
    float e = 600;
    
    
    void setup() {
      size(800, 800);
      background(255);
      stroke(0);
      fill(255);
      button1 = new Button1(0, 0, 400, 200);
    }
    
    void draw() {
    }
    
    class Button1 {
      float xpos;
      float ypos;
      float w;
      float h;
    
      Button1(float tempXpos, float tempYpos, float tempWidth, float tempHeight) {
        xpos = tempXpos;
        ypos = tempYpos;
        w = tempWidth;
        h = tempHeight;
      }
      void display() {
        stroke(0);
        fill(255);
        rectMode(CORNER);
      }
      void rectangle() {
        rect(xpos, ypos, w, h);
        if (mousePressed==true) {
          if (mouseX>xpos&&mouseX<xpos+200&&mouseY>ypos&&mouseY<ypos+200) {
            fill(0);
          }
        }
      }
    }
    class Button2 {
    } 
    

    With the initialiser you had:

    class Button1 {
      int xpos;
      int ypos;
      int w;
      int h;
    
      Button(float tempXpos, float tempYpos, float tempWidth, float tempHeight) {
    

    You needed:

    class Button1 {
      int xpos;
      int ypos;
      int w;
      int h;
    
      Button1(float tempXpos, float tempYpos, float tempWidth, float tempHeight) {
    

    Simple mistake :)

  • thanks man!!!!

Sign In or Register to comment.