构造函数未定义,Eclipse 说,但它已定义(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22493694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:04:29  来源:igfitidea点击:

Constructor undefined, says Eclipse, but its defined (Java)

javaeclipse

提问by user3435407

I don't get it, why is this constructor not defined. I check some other similar questions here, but there was always the problem, that they wanted to call the constructor without parameters, while the constructor had parameters. But I don't see this problem in my code. Could you please help me? Thank you!

我不明白,为什么没有定义这个构造函数。我在这里检查了一些其他类似的问题,但始终存在问题,他们想调用没有参数的构造函数,而构造函数有参数。但是我在我的代码中没有看到这个问题。请你帮助我好吗?谢谢!

I get the error message: The constructor Node(int) is undefined

我收到错误消息: The constructor Node(int) is undefined



The class with the main method:

带有 main 方法的类:

package LLP;

public class LinkedList2Test {

public void main (String args[]){
    LinkedList2 test = new LinkedList2();

    test.add(13);
    test.add(10);
    test.add(21);
}
}


The LinkedList2 class

LinkedList2 类

package LLP;

public class LinkedList2 {

Node head;
Node tail;

public void add(int data){

    **Node node = new Node(data);**// **THE PROBLEM is here**

    if (tail == null){
        tail = node;
        head = node;
    } else {
        tail.nextNode = node;
        tail=node;
    }
}
}


The Node class

节点类

package LLP;

public class Node {
int data;
Node nextNode;

public Node (int data){
    this.data = data;
}
}


As I see, in the main method I give in an integer, for example '13'. The add method receives this integer and calls it as 'data' And I would like to create the node with that 'data' Node's constructor needs just one integer, which would be 'data' so now 13 for example

正如我所见,在 main 方法中,我给出了一个整数,例如“13”。add 方法接收这个整数并将其称为“数据”我想用那个“数据”创建节点节点的构造函数只需要一个整数,这将是“数据”所以现在例如 13

Why does it not work, i dont get it...

为什么它不起作用,我不明白......

Many Thanks

非常感谢

采纳答案by Templar

It should work. I guess it could be that you didn't save your Node class after you provided a constructor hence the error.

它应该工作。我想可能是您在提供构造函数后没有保存 Node 类,因此出现错误。

Also as @mypal125 you probably want staticmain method in your LinkedList2Testclass. After changing that try to run (running also automatically saves all the changes) your program and see if there is still an error.

同样作为@mypal125,您可能希望static在您的LinkedList2Test班级中使用main 方法。更改后尝试运行(运行也会自动保存所有更改)您的程序,看看是否仍然存在错误。

回答by morenoadan22

The LinkedList2 class is missing the constructor.

LinkedList2 类缺少构造函数。

Add this to your LinkedList2 class:

将此添加到您的 LinkedList2 类:

public LinkedList2(int data){ }

回答by Igor Medvedyev

I had that problem a couple of times now with eclipse. What usually works is selecting a line where the error is and a sequence of

我现在用 eclipse 遇到过几次这个问题。通常有效的是选择错误所在的行和一系列

ctrl+x

ctrl+x

ctrl+v

Ctrl+V

回答by Mae Warren

This appears to be a glitch with Eclipse. For me it occurred following the renaming of the class being instantiated, and refactoring.

这似乎是 Eclipse 的一个小故障。对我来说,它发生在实例化类的重命名和重构之后。

For me Ctrl-x Ctrl-v worked only temporarily.

对我来说 Ctrl-x Ctrl-v 只是暂时起作用。

What worked for me was closing the Editor tab of the class being instantiated. In fact closing all tabs in the Eclipse editor (and reopening those you need) seems to clear Eclipse's confusion.

对我有用的是关闭正在实例化的类的编辑器选项卡。事实上,关闭 Eclipse 编辑器中的所有选项卡(并重新打开您需要的选项卡)似乎可以消除 Eclipse 的困惑。

回答by Joey B

In eclipse, the error “The constructor Node(int) is undefined” appeared after I right clicked on a folder and selected Build Path -> Use as Source Folder. To fix this issue, I followed the following steps:

在 Eclipse 中,在我右键单击一个文件夹并选择 Build Path -> Use as Source Folder 后出现错误“构造函数 Node(int) 未定义”。为了解决这个问题,我按照以下步骤操作:

  1. Rename the package containing the class(es) with the error
  2. Create a new folder using the old folder (package) name
  3. Create any preexisting subfolders under the folder just created
  4. Move the old class(es) from the renamed package to its rightful place in the new folder structure
  5. Once all the classes have been moved from the renamed package to the new folder structure, delete the package
  6. Perform a Project -> Clean
  1. 重命名包含错误的类的包
  2. 使用旧文件夹(包)名称创建一个新文件夹
  3. 在刚刚创建的文件夹下创建任何预先存在的子文件夹
  4. 将旧类从重命名的包移动到新文件夹结构中的正确位置
  5. 将所有类从重命名的包移动到新文件夹结构后,删除包
  6. 执行项目 -> 清洁