Delphi Error – E2082 TYPEOF can only be applied to object types with a VMT

Delphi Compiler Error

E2082 TYPEOF can only be applied to object types with a VMT

Reason for the Error & Solution

This error message is issued if you try to apply the standard function TypeOf to an object type that does not have a virtual method table.

A simple workaround is to declare a dummy virtual procedure to force the compiler to generate a VMT.

program Produce;

type
  TMyObject = object
    procedure MyProc;
  end;

procedure TMyObject.MyProc;
begin
  (*...*)
end;

var
  P: Pointer;
begin
  P := TypeOf(TMyObject);    (*<-- Error message here*)
end.

The example tries to apply the TypeOf standard function to type TMyObject which does not have virtual functions, and therefore no virtual function table (VMT).

program Solve;

type
  TMyObject = object
    procedure MyProc;
    procedure Dummy; virtual;
  end;

procedure TMyObject.MyProc;
begin
  (*...*)
end;

procedure TMyObject.Dummy;
begin
end;

var
  P: Pointer;
begin
  P := TypeOf(TMyObject);
end.

The solution is to introduce a dummy virtual function, or to eliminate the call to TypeOf.

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