Talaria
Talaria

Reputation: 228

Error with two different generic types

So, I understand this error message is telling me what is wrong, but I am having trouble determining why I am coming up with this error and how to resolve it:

constructor Node in class Node cannot be applied to given types required: E#1 found: no arguments reason: actual and formal argument lists differ in length where E#1,E#2 are type-variables: E#1 extends Object declared in class MyStack E#2 extends Object declared in class Node

I have implemented stacks in the past with linked lists and never ran into this before. I have included the first sections of code for my MyStack class and Node class, as I don't believe the methods contained are relevant. If they are I'm happy to edit them in. My main issue is I don't understand why it is making two separate generic types. I understand it is telling me that actual and formal argument lists differ in length, but how can that be and what could I do to resolve this?

public class MyStack<E> extends Node<E>{

//pieced together linked list
private int cnt;
private Node<E> head;


public MyStack() {
    head = null;
    cnt = 0;
}

Here is the Node Class.

public class Node <E>{ 



public Node<E> link;
public E item;

public Node(E data) {
    item = data;
    link = null;
}

Any clues as to why this is showing 2 different generic types would be helpful. Cheers!

Upvotes: 0

Views: 881

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79847

As there is no no-argument constructor for Node, and MyStack extends Node, you'll need to specify how the constructor for MyStack is going to call the constructor for Node. Currently, this won't compile because you don't have the super constructor call at the start of the constructor for MyStack.

Upvotes: 4

Related Questions