Delphi Error – E2568 Can’t create new instance without CONSTRUCTOR constraint in type parameter declaration

Delphi Compiler Error

E2568 Can’t create new instance without CONSTRUCTOR constraint in type parameter declaration

Reason for the Error & Solution

This occurs when the generic type is derived from a class that has an implicit constructor and the default constructor clause is not mentioned.

program E2568;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
TMyClass1 = class

end;
TMyClass2<T: TMyClass1> = class // Fix: replace < T : TmyClass1> with <T : TMyClass1, constructor> 
public
procedure Add;
end;

procedure TMyClass2<T>.Add;
begin
T.Create(nil); //E2568
end;

begin
  Writeln('E2568 Cannot create new instance without CONSTRUCTOR constraint in type parameter declaration');
end.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error E2313 Attribute – Known attribute cannot specify properties Reason for the Error & Solution No further information...
Delphi Compiler Error E2379 Virtual methods not allowed in record types Reason for the Error & Solution No further information...
Rodrigo , one of the long time Delphi Developer has been working on one of his personal project “Delphi IDE...