Class “Nebula” does not exist

2019-03-02 04:59发布

问题:

I am having issues with Processing 3.3. I am just starting on a type of nebula simulator, meant to simulate the birth and life cycle of a star from a nebula to red giant. I have created two classes so far: Gas, for each individual gas particle, and Nebula, referring to the collection of particles. I have typed in the following code to the editor with the same result each time: 'Class "Nebula" does not exist.' My code, drastically simplified, is as follows:

Gas:

class Gas {
  /* variables, constructor, etc. */

  void applyGravity(Nebula n) {
    /* code to apply force of gravity of
       the nebula to the particle */
  }
}

Nebula:

class Nebula {
  ArrayList<Gas> particles; // array of particles

  /* variables, constructor, etc. */
}

Oddly enough, I don't get the error that 'Class "Gas" does not exist' in the Nebula class, but I do get the error 'Class "Nebula" does not exist' in the Gas class.

I have tried exiting and reopening the files, as well as reinstalling Processing. Any help would be greatly appreciated.

回答1:

Basically, the Processing editor can handle two types of code. The first type is a basic list of function calls, like this:

size(500, 200);
background(0);
fill(255, 0, 0);
ellipse(width/2, height/2, width, height);

With this type of code, Processing simply runs the commands one at a time.

The second type of code is a "real" program with function definitions, like this:

void setup(){
   size(500, 200);
}

void draw(){
    background(0);
    fill(255, 0, 0);
    ellipse(width/2, height/2, width, height);
}

With this type of code, Processing calls the setup() function once at the beginning, and then calls the draw() function 60 times per second.

But note that you can't have code that mixes the two types:

size(500, 200);

void draw(){
    background(0);
    fill(255, 0, 0);
    ellipse(width/2, height/2, width, height);
}

This will give you a compiler error, because the size() function is not inside a function!

What's going on with your code is Processing sees that you haven't defined any sketch-level functions, so it tries to treat it as the first type of code. But then it sees class definitions, which are only valid in the second type of code. That's why you're getting an error.

To fix your problem, just define a setup() and/or draw() function in your code, so Processing knows it's a "real" program.