简体   繁体   中英

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. When I try to create a new instance in another class with one int parameter, it is giving me that error. 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 ... ) .

Since MyPriorityQueue is a dHeap , you'll likely want to remove field queue , and replace queue = new dHeap<T>(queueSize); with super(queueSize) .

You'll also likely want to remove field size .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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