gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:22:08 33 4
gpt4 key购买 nike

有人向我介绍了 TObjectList,我想使用它,但我似乎无法获得 sample code from Embarcadero's web site为我工作。这是我的代码:

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.

尝试编译此文件时,出现错误 [DCC Error] Test03Unit1.pas(51): E2003 未声明的标识符:'TObjectList<>'。第 51 行内容如下:

List: TObjectList<TNewObject>;

我以前从未见过 Pascal 语言中使用过 < >,所以这对我来说是全新的领域。在谷歌上搜索“Delphi and < >”似乎并没有告诉我我需要了解什么。来自 other examples I can find on the internet这似乎是正确的使用方法。

使用德尔福XE2。

我做错了什么?

最佳答案

您必须添加 System.Generics.Collections 到您的使用条款。这是声明 TObjectList<T> 的单位.

我已将文档链接添加到答案中。当您找不到某个类时,请在文档中查找。这将告诉您需要使用哪个单位。

以及 TObjectList<T>TList<T> 。使用 TObjectList<T> 是有意义的当您希望列表拥有其成员时。否则,使用TObjectList<T>没有任何好处。你也可以使用TList<T> 。除了内置的Delphi通用容器之外,还有许多优秀的第三方容器,它们往往更优秀。例如,Delphi Spring Framework有一个很好的容器集合。

关于delphi - TObjectList E2003 未声明的标识符 TObjectList<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20076655/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com