E2021 Class type required (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)


In certain situations the compiler requires a 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 TObject. 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 IInterface, which is the root of all interfaces. A convenience class exists, TInterfacedObject, 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 Raising an Exception.