Ich habe probleme bei der umsetzung von generics in fpc. Ich möchte einen generischen Stack machen :
Code: Alles auswählen
unit stack;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TStack }
generic TStack<T> = class(TObject)
var private
thisStack : array of T;
var public
function empty():boolean;
function peek():T;
function pop():T;
procedure push(entry : T);
procedure setEmpty();
end;
implementation
{ TStack }
function TStack.empty(): boolean;
begin
result := length(thisStack) = 0;
end;
function TStack.peek(): T;
begin
result := thisStack[length(thisStack)-1];
end;
function TStack.pop(): T;
begin
result := thisStack[length(thisStack)-1];
SetLength(thisStack,length(thisStack)-1);
end;
procedure TStack.push(entry: T);
begin
SetLength(thisStack,length(thisStack)+1);
thisStack[length(thisStack)-1] := entry;
end;
procedure TStack.setEmpty();
begin
SetLength(thisStack,0);
end;
end.
Code: Alles auswählen
procedure TFrmMain.BtnStackTestClick(Sender: TObject);
type
TStringStack = specialize TStack<String>;
Var
myStack : TStringStack;
begin
//myStack := TstringStack.Create;
//myStack.push('test');
//ShowMessage(myStack.peek());
//ShowMessage(myStack.pop());
//ShowMessage(BoolToStr(myStack.empty()));
end;
stack.pas(1,1) Fatal: CompilAn unhandled exception occurred at $00450370 :
Was mache ich da Falsch?
Danke für Antworten.