Trouble initializing a templated class

quakkels via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jul 5 13:03:20 PDT 2014


> ah, sorry, I misunderstood. It looks like you need to change 
> the lin
>
> auto sc = new SomeClass ();
>
> to
>
> auto sc = new SomeClass!BaseClass ();
>
> The compiler complains because SomeClass is a template when you 
> call SomeClass() without !() template parameters. It only 
> becomes a type once instantiated with parameters.

Thanks. That did it.

Here's my working program.

	public class BaseClass {}
	public class OtherClass : BaseClass {}

	class SomeClass(T : BaseClass)
	{
		public T[] values;
		public void add(T input) { values ~= input; }
	}

	void main()
	{
		auto sc = new SomeClass!BaseClass();
		OtherClass oc1 = new OtherClass();
		OtherClass oc2 = new OtherClass();

		sc.add(oc1);
		sc.add(oc2);

		import std.stdio;
		writefln("value count: %d", sc.values.length);
	}


More information about the Digitalmars-d-learn mailing list