7

Hi Stack Overflow community ^_^

Basically, I am working with Stacks and converting from Infix to Postfix equations. This is the error message showing on screen when I am trying 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 troubles trying to figure out this error but sadly I don't have a clue of what might be the problem. If I knew a little better I could have provided you with more info.

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();
    }
}
saqehi
  • 111
  • 1
  • 1
  • 4

1 Answers1

7

It seems you have a class LinkedList declared as

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

but you're trying to use it in a class Stack declared as

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

The type variable T declared in Stack is completely unrelated to the type variable T in LinkedList. What's more, they are not compatible. LinkedList expects a type that is a sub type of Comparable, but Stack is giving it a type argument that has no bounds. The compiler cannot allow this.

Add appropriate bounds to your Stack class' type parameter

class Stack<T extends Comparable<T>> {
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724