Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't compile class with an interface

I am trying to create a class that implements an interface but I get these errors:

[dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface.QueryInterface
[dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface._AddRef
[dcc32 Error] dl_tPA_MailJournal.pas(10): E2291 Missing implementation of interface method IInterface._Release
[dcc32 Fatal Error] MainUnit.pas(8): F2063 Could not compile used unit 'dl_tPA_MailJournal.pas'

The code is:

unit dl_tPA_MailJournal;

interface

uses
  Windows,
  Generics.Collections,
  SysUtils,
  uInterfaces;

type
  TtPA_MailJournal = class(TObject, ITable)
  public
    function GetanQId: integer;
    procedure SetanQId(const Value: integer);
    function GetadDate: TDateTime;
    procedure SetadDate(const Value: TDateTime);

    function toList: TList<string>;

    constructor Create(aId : Integer; aDate : TDateTime);

  private
    property anQId : integer read GetanQId write SetanQId;
    property adDate : TDateTime read GetadDate write SetadDate;
end;

implementation

{ TtPA_MailJournal }

constructor TtPA_MailJournal.Create(aId : Integer; aDate : TDateTime);
begin
  SetanQId(aId);
  SetadDate(aDate);
end;

function TtPA_MailJournal.GetadDate: TDateTime;
begin
  Result := adDate;
end;

function TtPA_MailJournal.GetanQId: integer;
begin
  Result := anQId ;
end;

procedure TtPA_MailJournal.SetadDate(const Value: TDateTime);
begin
  adDate := Value;
end;

procedure TtPA_MailJournal.SetanQId(const Value: integer);
begin
  anQId := Value;
end;

function TtPA_MailJournal.toList: TList<string>;
var
  aListTable: TList<TtPA_MailJournal>;
  aTable: TtPA_MailJournal;
  aListString: TList<String>;
begin
  aTable.Create(1,now);
  aListTable.Add(aTable);
  aTable.Create(2,now);
  aListTable.Add(aTable);
  aListString.Add(aListTable.ToString);

  Result := aListString;
end;

end.

And the interface is:

unit uInterfaces;

interface

uses
  Generics.Collections;

type
  ITable = Interface
    ['{6CED8DCE-9CC7-491F-8D93-996BE8E4D388}']
    function toList: TList<string>;
  end;

implementation

end.
like image 470
ververicka Avatar asked Dec 10 '22 21:12

ververicka


1 Answers

The problem is that you use TObject as the parent for your class. You should use TInterfacedObject instead.

In Delphi, every interface inherits from IInterface at therefore has, at least, the following 3 methods:

  • _AddRef
  • _Release
  • QueryInterface

You must implement these 3 methods, either by implementing them yourself or by inheriting from a base object that includes these methods.

Because you inherit from TObject, but you are not implementing these 3 methods, you get a compilation error. If you read the compiler error, you will see that it actually spells out this omission for you.

TInterfacedObject has already implemented these methods for you.
Other base objects that implement IInterface (aka IUnknown) are: TAggregatedObject and TContainedObject. However these are special purpose vehicles, only to be used if you really know what you're doing.

Change the definition of your class to

TTPA_MailJournal = class(TInterfacedObject, ITable)

And your code will compile.

See Delphi basics for more info.

like image 126
Johan Avatar answered Dec 20 '22 06:12

Johan