Trouble with SList for generic Stack class

Mark via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 2 15:21:07 PDT 2017


Hello,

I am trying to make a class that can accept any type as an 
argument.

Here is the class:


import std.container: SList;


class Stack {

   private SList!T list;
   private int _size;

   this(T)() {
     list = SList!T;
     _size = 0;
   }

   public void push(T)(T item) {
     list.insertFront(item);
     _size++;
   }

   public void pop(T)() {
     if(_size > 0){
       list.removeFront();
       _size--;
     }
   }

   public int getSize() { return _size; }

}


When I compile with dmd, I get this error:

Stack.d(6): Error: undefined identifier 'T'

That's on the line private SList!T list;

So the SList needs a type, but I can't give that type to it at 
that moment when I instantiate the class.
How do I over come this problem?

I might just write this again, using structs as a SLL. I might 
have similar issues with that as well.

Thanks,
Mark.


More information about the Digitalmars-d-learn mailing list