简体   繁体   English

没有找到适合 java 的构造函数

[英]No suitable constructor found for java

I am getting the error message "no suitable constructor found" when the constructor I am trying to use only has one int parameter.当我尝试使用的构造函数只有一个int参数时,我收到错误消息“找不到合适的构造函数”。 When I try to create a new instance in another class with one int parameter, it is giving me that error.当我尝试使用一个int参数在另一个类中创建一个新实例时,它给了我那个错误。 What am I doing incorrectly to cause this error?我在做什么不正确导致此错误?

public class dHeap <T extends Comparable <? super T>> implements dHeapInterface<T> {
  
    private int children;
    private T[] array;
    private int size;
  
    public dHeap (int heapSize){
       array = (T[]) new Comparable[heapSize];
       children = 2;
       size = 0;
    }
    public dHeap (int d, int heapSize) { 
       array = (T[]) new Comparable[heapSize];
       children = d;
       size = 0;
    }
...
}

public class MyPriorityQueue<T extends Comparable <? super T>> extends dHeap<T> {
  private dHeap<T> queue;
  private int size;
  
  public MyPriorityQueue(int queueSize)
  {
    queue = new dHeap<T>(queueSize);
    size = 0;
  }
...
}

the error is错误是

no suitable constructor found for dHeap()
constructor dHeap.dHeap(int,int) is not applicable
  (actual and formal argument lists differ in length)
constructor dHeap.dHeap(int) is not applicable
  (actual and formal argument lists differ in length)

在类 dHeap 的代码中,您没有创建任何默认构造函数,但是在 MyPriorityQueue 中,您正在扩展它,并且构造函数调用了它的超级默认构造函数,在这种情况下不存在。

Since MyPriorityQueue extends dHeap , and dHeap doesn't have a no-arg constructor, you must call an appropriate constructor using super( ... args here ... ) .由于MyPriorityQueue扩展了dHeap ,并且dHeap没有无参数构造函数,因此您必须使用super( ... args here ... )调用适当的构造函数。

Since MyPriorityQueue is a dHeap , you'll likely want to remove field queue , and replace queue = new dHeap<T>(queueSize);由于MyPriorityQueuedHeap ,您可能希望删除字段queue ,并替换queue = new dHeap<T>(queueSize); with super(queueSize) .super(queueSize)

You'll also likely want to remove field size .您可能还想删除 field size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM