It doesn't seem like just a matter of the name of tabs.. I think there must be something else.

edited September 2017 in Questions about Code

Hello, I realized that I need to have an appropriate tab name for each tab.

However, I think I still have a hard time with execute the following code below. I really don't know why it keeps saying constructor was not defined yet.

  1. sketch tab coding ArrayList particles;

    void setup() {
      size(640, 360);
      particles = new ArrayList<Particle>();
    
      for (int i =0; i<10; i++) {
        particles.add(new Particle());
    }
    }
      void draw() {
        background(255);
    
      for(Particle p: particles) {
        p.update();
        p.display();
      }
      }
    
  2. Class tab class Particle {

      PVector location;
      PVector velocity;
      PVector acceleration;
    
      Particle(PVector l) {
        acceleration = new PVector(0, 0.05);
        velocity = new PVector(random(-1,1),random(-1,1));
        location = l.get();
      }
    
      void update() { 
        velocity.add(acceleration);
        location.add(velocity);
      }
    
      void display() {
        stroke(0);
        strokeWeight(2);
        fill(127);
        ellipse(location.x,location.y,12,12);
      }
    }
    

The error message is like below error1 error2

Tagged:

Answers

  • You're not passing a PVector

  • edited September 2017 Answer ✓
    • In Java, every function has something called signature.
    • And the same function name can have multiple signatures that's called overloading.
    • Each signature has its own number of parameters with their datatypes, and the order they are requested, plus its returning datatype.
    • Now, your class Particle's constructor function got 1 signature only.
    • And that signature is 1 parameter of datatype PVector: Particle(PVector l) {}.
    • So, in order to instantiate a Particle w/ operator new, you must also pass to it a PVector argument! L-)
  • ... I realized that I need to have an appropriate tab name for each tab.

    • Actually, as I had explained already in your previous post, only the 1st tab's name is important.
    • The other tabs, as long as they're ".pde", can have any OS valid name. :ar!
  • edited September 2017 Answer ✓

    The reason these tab names don't matter is that, other than your first tab, the other tab names don't even appear in the code that Java compiles. They are like a stack of paper that then gets treated as one long single sheet -- and the name of that document is the top file name (the first tab).

  • edited September 2017

    @GoToLoop

    Thank you so much always. So you mean I didn't pass my PVector into other functions like update and display?

    But even though I tried to pass my PVector to other functions, it still doesn't work.

    If possible, can you show me how to fix this code? I mean, can you show me the revised correct code for my wrong code?

    The thing I tried to assign signatures other functions is as below. class Particle {

              PVector location;
              PVector velocity;
              PVector acceleration;
    
              Particle(PVector l) {
                acceleration = new PVector(0, 0.05);
                velocity = new PVector(random(-1,1),random(-1,1));
                location = l.get();
              }
             // I understood putting PVector into the bracket is
                 assigning PVector into the function. but I think its wrong..
    
              void update(PVector l) { 
                velocity.add(acceleration);
                location.add(velocity);
              }
    
              void display(Pvector l) {
                stroke(0);
                strokeWeight(2);
                fill(127);
                ellipse(location.x,location.y,12,12);
              }
            }
    

    Can you let me know how to fix this problem? Always thank you.

  • That's not right, the problem is on line 6 in the other file.

  • edited September 2017

    The bug is in particles.add(new Particle());, where you attempt to instantiate class Particle passing nothing to it, while its signature demands 1 PVector argument to be passed to it! :-B

    Here's a workaround so it, at least, compiles: particles.add(new Particle(new PVector()));

    However, an empty PVector doesn't make much sense, given its 3 fields x, y & z are all 0.0. :-@

Sign In or Register to comment.