Delphi Error – E2021 Class type required

Delphi Compiler Error

E2021 Class type required

Reason for the Error & Solution

In certain situations the compiler requires a class type:

  • .
  • In the on-clause of a try-except statement.
  • .
  • As the final type of a forward declared class type.

Class Type Required on a Class Declaration

You get this error if you try to declare a class with no parent class that does implement one or more interfaces. For example:

TMyClass = class(IMyInterface1, IMyInterface2);

In Delphi every class inherits from some parent class, and if you do not specify a parent class when you declare your class, the compiler assumes that your class inherits from . In other words, the following two lines of code are equivalent:

TMyClass = class;
TMyClass = class(TObject);

To fix your code, you must specify a parent class before the interfaces that your class implements:

TMyClass = class(TObject, IMyInterface1, IMyInterface2);

However, if you inherit directly from TObject you have to implement the API of , which is the root of all interfaces. A convenience class exists, , that implements that API for you:

TMyClass = class(TInterfacedObject, IMyInterface1, IMyInterface2);

Class Type Required on a Raise Statement

You get this error if you try to raise a string literal. For example:

program Produce;
begin
  raise 'This would work in C++, but does not in Delphi';
end.

You must create an exception object instead:

program Solve;
uses SysUtils;
begin
  raise Exception.Create('There is a simple workaround, however');
end.

For more information, see .

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...