[英]Java - symbol not found (constructor)
So I'm writing some code that reads from a file: 所以我正在编写一些从文件中读取的代码:
array[k] = Salesperson(infile.nextInt(), infile.nextInt(), myName);
I wrote a constructor for Salesperson that looks somewhat likes this: 我为Salesperson编写了一个看起来有点像这样的构造函数:
public Salesperson(int cheese, int butter, String name)
When I try to compile (first Salesperson, then the actual program), I get this: 当我尝试编译(第一个Salesperson,然后是实际的程序)时,我得到了这个:
program.java:39: cannot find symbol
symbol : method Salesperson(int,int,java.lang.String)
You're missing the new keyword. 你错过了新关键字。 eg 例如
array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);
This is resulting in the compiler attempting to find a method called Salesperson that returns a type of Salesperson, which would be invalid anyway. 这导致编译器试图找到一个名为Salesperson的方法,该方法返回一种Salesperson类型,无论如何都是无效的。
Use the new keyword. 使用new关键字。 You should do it: 你应该这样做:
array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);
You can't assign without the new keyword because it's not a method where you can return a value. 如果没有new关键字,则无法分配,因为它不是可以返回值的方法。
As I see it, you have declared an array
of Salesperson
objects and you want to put data into it from a file. 在我看来,你已经声明了一个Salesperson
对象array
,并且你想从文件Salesperson
数据放入其中。 What you are missing is the new
keyword. 您缺少的是new
关键字。 Using new
keyword creates a new object of the class and calls the constuctor
in the process. 使用new
关键字创建类的新对象并在进程中调用constuctor
函数。 You may use the follwing code: 您可以使用以下代码:
array[k] = new Salesperson(infile.nextInt(), infile.nextInt(), myName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.