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 .