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 X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...