类“星云”不存在 [英] Class "Nebula" does not exist

查看:88
本文介绍了类“星云”不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理3.3时遇到问题。我只是从一种星云模拟器开始,它旨在模拟恒星从星云到红色巨星的诞生和生命周期。到目前为止,我已经创建了两类:针对每个单独的气体粒子的气体,以及涉及粒子集合的星云。我每次都在编辑器中键入以下代码,并得到相同的结果:类 Nebula不存在。我的代码经过了大幅简化,如下所示:

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 */
  }
}

星云:

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.

我尝试退出并重新打开文件,以及重新安装Processing。任何帮助将不胜感激。

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

推荐答案

基本上,Processing编辑器可以处理两种类型的代码。第一种是函数调用的基本列表,例如:

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);

使用这种类型的代码,Processing一次只运行一个命令。

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);
}

使用这种类型的代码,Processing调用设置()函数开始,然后每秒调用 draw()函数60次。

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);
}

这会给您一个编译器错误,因为 size()函数不在函数内部!

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.

要解决您的问题,只需定义 setup()和/或代码中的 draw()函数,因此Processing知道它是一个真实程序。

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

这篇关于类“星云”不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆