简体   繁体   English

TObjectList E2003未声明的标识符TObjectList <>

[英]TObjectList E2003 Undeclared identifier TObjectList<>

I've been introduced to TObjectList, and I would like to make use of it, except that I can't seem to get even the sample code from Embarcadero's web site to work for me. 我已经被介绍给TObjectList,并且我想利用它,除了我似乎甚至无法从Embarcadero网站上获得示例代码来为我工作。 Here is my code: 这是我的代码:

unit Test03Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  { Declare a new object type. }
  TNewObject = class
  private
    FName: String;

  public
    constructor Create(const AName: String);
    destructor Destroy(); override;
  end;

{ TNewObject }

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TNewObject.Create(const AName: String);
begin
  FName := AName;
end;

destructor TNewObject.Destroy;
begin
  { Show a message whenever an object is destroyed. }
  MessageDlg('Object "' + FName + '" was destroyed!', mtInformation, [mbOK], 0);
  inherited;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TObjectList<TNewObject>;
  Obj: TNewObject;
begin
  { Create a new List. }
  { The OwnsObjects property is set by default to true -- the list will free the owned objects automatically. }
  List := TObjectList<TNewObject>.Create();

  { Add some items to the List. }
  List.Add(TNewObject.Create('One'));
  List.Add(TNewObject.Create('Two'));

  { Add a new item, but keep the reference. }
  Obj := TNewObject.Create('Three');
  List.Add(Obj);

  {
    Remove an instance of the TNewObject class. Destructor
    is called for the owned objects, because you have set the OwnsObjects
    to true.
  }
  List.Delete(0);
  List.Extract(Obj);

  { Destroy the List completely--more message boxes will be shown. }
  List.Free;

end;

end.

When trying to compile this, I get the error [DCC Error] Test03Unit1.pas(51): E2003 Undeclared identifier: 'TObjectList<>'. 尝试编译此错误时,出现错误[DCC错误] Test03Unit1.pas(51):E2003未声明的标识符:'TObjectList <>。 Line 51 is the line which says: 第51行是这样写的:

List: TObjectList<TNewObject>;

I have never seen < > used in the Pascal language before, so this is completely new territory for me. 我以前从未见过在Pascal语言中使用过<>,因此对我来说这是全新的领域。 Doing a google search for "Delphi and < >" doesn't seem to tell me what I need to know about this. 用谷歌搜索“ Delphi和<>”似乎并不能告诉我我需要知道些什么。 From other examples I can find on the internet it seems to be the correct way to use it. 其他示例中,我可以在互联网上找到它,似乎是使用它的正确方法。

Using Delphi XE2. 使用Delphi XE2。

What am I doing wrong? 我究竟做错了什么?

You must add System.Generics.Collections to your uses clause. 您必须将System.Generics.Collections添加到uses子句。 That is the unit which declares TObjectList<T> . 那就是声明TObjectList<T>

I've added documentation links to the answer. 我已经为答案添加了文档链接。 When you cannot find a class, look for it in the documentation. 当找不到类时,请在文档中查找。 That will tell you which unit you need to use. 这将告诉您您需要使用哪个单位。

As well as TObjectList<T> there is TList<T> . TObjectList<T>还有TList<T> It makes sense to use TObjectList<T> when you want the list to own its members. 当您希望列表拥有其成员时,可以使用TObjectList<T> Otherwise, there is no benefit in using TObjectList<T> and you may as well use TList<T> . 否则,使用TObjectList<T>没有任何好处,而您也可以使用TList<T> As well as the built-in Delphi generic containers, there are many excellent third-party containers that are often superior. 除了内置的Delphi通用容器之外,还有许多优秀的第三方容器,它们通常是上乘的。 For instance, the Delphi Spring Framework has a fine collection of containers. 例如, Delphi Spring框架具有很好的容器集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM