The type argument is not within the scope of the type variable.

Hello community ^ _ ^

Basically, I work with Stacks and converts from Infix to Postfix equations. This is the error message displayed on the screen when I try to compile the Stack class:

Stack.java:5: error: type argument T#1 is not within bounds of type-variable T#2
    private LinkedList<T> list;
                       ^
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in class Stack
    T#2 extends Comparable<T#2> declared in class LinkedList

I am having real problems trying to figure this error out, but unfortunately I don't know what could be the problem. If I knew a little better, I could provide you with additional information.

Thanks in advance for any comments and help!

Update: here is my class ...

package ListPkg; 

public class Stack<T> //    implements Comparable<Stack>>  
{ 
    private LinkedList<T> list; 

    public Stack()
    {
        this("list");
    }
    public Stack(String name)
    {
        list = new LinkedList(name);
    }
    public void push(T item)
    {
        list.insertAtFront(item);
    }
    public T pop()
    {
        list.removeFromFront();
    }
    public int lenghtIs()
    {
        return list.lengthIs();
    }
    public T peek()
    {
        return list.returnFirstNode();
    }
    public void print()
    {
        list.print();
    }
    public boolean isEmpty()
    {
        return list.isEmpty();
    }
}
+4
source share
1 answer

It seems you have a class LinkedListdeclared as

class LinkedList<T extends Comparable<T>> {...}

but you are trying to use it in a class Stackdeclared as

class Stack<T> {
    private LinkedList<T> list;
    ...
}

T, Stack, T LinkedList. , . LinkedList , Comparable, Stack , . .

Stack class

class Stack<T extends Comparable<T>> {
+4

Source: https://habr.com/ru/post/1533450/


All Articles