Missing Left Curly Bracket?

Hi, I'm kinda new to processing and I don't get why the compiler doesn't like this really simple code:

` Icon icon;

void setup(){ size(600, 650); rectMode(CENTER);

icon = new Icon(25, height - 50); }

void draw(){ background(0); fill(255);

icon.show(); }

void keyReleased(){ if(keyCode == LEFT)icon.X -= 50; if(keyCode == RIGHT)icon.X += 50; }

class Icon { float X; float Y;

Icon(float x, float y){ X = x; Y = y; } // compiler says: Missing left curly bracket "{"

if(X < 25)X = width - 25; else if(X > width - 25)X = 25;

void show(){ rectMode(CENTER); fill(255); rect(X, Y, 50, 50); } } `

It says Missing left curly bracket "{" when I really don't think I am. Does anyone know what is going on? Thanks

Answers

  • Answer ✓

    The compiler is doing its best to tell you that there is a structural problem with your code. The fact that there is a problem is often actually more useful that what the compiler thinks the problem is.

    In short, you are not missing a {. But you do have a problem: You have some code inside your class that is not in a function! Look:

    Icon icon;
    
    void setup() { 
      size(600, 650); 
      rectMode(CENTER);
      icon = new Icon(25, height - 50);
    }
    
    void draw() { 
      background(0); 
      fill(255);
      icon.show();
    }
    
    void keyReleased() { 
      if (keyCode == LEFT)icon.X -= 50; 
      if (keyCode == RIGHT)icon.X += 50;
    }
    
    class Icon { 
      float X; 
      float Y;
    
      Icon(float x, float y) { 
        X = x; 
        Y = y;
      } // The error is here, yes..
    
      // .. but the problem is here! What function is this code in?
      if (X < 25)X = width - 25; 
      else if (X > width - 25)X = 25;
    
      void show() { 
        rectMode(CENTER); 
        fill(255); 
        rect(X, Y, 50, 50);
      }
    }
    

    Proper formatting helps make this apparent easily. Press Ctrl + t to auto format your code. Also learn how to post code here on the forums properly by reading the sticky about it.

  • I see what you mean. I'm really new to classes so I never thought about that. Thanks

Sign In or Register to comment.