Java“类中的构造函数不能应用于给定类型"“必需:找不到参数:字符串" [英] Java 'constructor in class cannot be applied to given types' 'required: no arguments found: String'
问题描述
我正在处理一项作业,我必须从头开始创建一个链表,并且在编译时遇到一个错误:Node 类中的构造函数 Node 无法应用于给定类型;"
I'm working on an assignment where I have to create a linked list from scratch and have come across an error when compiling that the "constructor Node in class Node cannot be applied to given types;"
这就是我正在尝试的,错误说:
This is what I'm trying, the error says:
必需:无参数找到:字符串
required: no arguments found: string
但是我看不出哪里出错了,因为我的 Node 构造函数需要一个字符串?
Yet I cannot see where I am going wrong as my constructor for Node requires a string?
public class Node {
String data;
Node next;
public void Node(String x) {
data = x;
next = null;
}
}
public class stringList {
private Node head;
private int count;
public void stringList() {
head = null;
count = null;
}
public void add(String x) {
Node temp = new Node(x);
}
推荐答案
这个:
public void Node(String x) {
data = x;
next = null;
}
应该是:
public Node(String x) {
data = x;
next = null;
}
目前您有一个 default 构造函数(不带参数),它是在没有任何显式构造函数的情况下隐式定义的.
Currently you have a default constructor (taking no arguments), which is implicitly defined in the absence of any explicit constructors.
这篇关于Java“类中的构造函数不能应用于给定类型"“必需:找不到参数:字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!