Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Delphi中将自定义比较器传递给通用创建过程_Delphi_Generics_Delphi 10 Seattle - Fatal编程技术网

在Delphi中将自定义比较器传递给通用创建过程

在Delphi中将自定义比较器传递给通用创建过程,delphi,generics,delphi-10-seattle,Delphi,Generics,Delphi 10 Seattle,我正在试验Delphi10,并试图创建我的第一个通用容器类。我需要一个通用比较器的帮助 这里是我创建的一个简单哈希对象: type TsmHeap<T> = class private fList: TList<T>; Comparer: TComparer<T>; procedure GetChildren(ParentIndex: integer; var Child1, Child2: integer); func

我正在试验Delphi10,并试图创建我的第一个通用容器类。我需要一个通用比较器的帮助

这里是我创建的一个简单哈希对象:

type
  TsmHeap<T> = class
  private
    fList: TList<T>;
    Comparer: TComparer<T>;
    procedure GetChildren(ParentIndex: integer; var Child1, Child2: integer);
    function GetParent(ChildIndex: integer): integer;
    function GetCapacity: integer;
    function GetCount: integer;
    function MustSwap(iParent, iChild: integer): boolean;
    procedure SetCapacity(const Value: integer);
  public
    constructor Create(aComparer: TComparer<T>); overload;
    constructor Create(aComparer: TCOmparer<T>; aCapacity: integer); overload;

    destructor Destroy; override;

    //-- Methods & Functions
    function Dequeue: T;
    procedure Enqueue(Item: T);
    function IsEmpty: boolean;

    //-- Properties
    property Count: integer read GetCount;
    property Capacity: integer read GetCapacity write SetCapacity;
  end;
类型
TsmHeap=类
私有的
fList:TList;
比较者:t比较者;
过程GetChildren(ParentIndex:integer;var Child1,Child2:integer);
函数GetParent(ChildIndex:integer):整数;
函数GetCapacity:整数;
函数GetCount:integer;
函数MustSwap(iParent,iChild:integer):布尔值;
程序设置能力(常量值:整数);
公众的
构造函数创建(a比较者:t比较者);超载;
构造函数创建(aComparer:TCOmparer;aCapacity:integer);超载;
毁灭者毁灭;推翻
//--方法与功能
函数出列:T;
程序排队(项目:T);
函数为空:布尔;
//--性质
属性计数:整数读取GetCount;
属性容量:整数读取GetCapacity写入SetCapacity;
结束;
我已经为这些方法编写了代码,它可以自己编译,没有问题。然而,当我试图创建该类的整数版本时,我无法编译它

问题代码是:

iHeap := TsmHeap<integer>.Create(TComparer<integer>.Construct(
  function(const Left, Right: integer): integer
  begin
    result := Sign(Left - Right);
  end)
);
iHeap:=TsmHeap.Create(TComparer.Construct(
函数(常量左、右:整数):整数
开始
结果:=符号(左-右);
(完)
);
这给出了一个“E2250没有重载版本的‘Create’可以用这些参数调用”


我做错了什么?如何创建比较器?

TComparer.Construct
返回
IComparer
-它是一个类函数,而不是构造函数。只需将
TsmHeap.Create
的参数类型更改为
IComparer
,它应该可以工作。

此外:我认为私有字段
Comparer
需要相同的类型
IComparer
。但是没有理由存储它,因为TList可以存储比较器本身。
Sign
没有任何用途,而且是浪费。此外,减法很容易溢出。这是实现比较器的错误方法。