Skip to content
Advertisement

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)

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement