[英]Spring4D TMultiMap overloaded constructor not seen by compiler (Error E2250)
我正在努力使用 Spring4D (1.2.2) TMultiMap 通用 class。 我想調用一個重載的構造函數,編譯器抱怨:
"E2250 There is no overloaded version of 'Create' that can be called with these arguments"
根據 Spring4D 源代碼,參數是正確的類型。
我設計了一個小程序來重現錯誤:
program Spring4DMultiMapTest;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Generics.Collections,
Spring.Tests.Interception.Types,
Spring.Collections.MultiMaps,
Spring.Collections;
type
TMyKey = TPair<Double, Integer>;
TMyKeyEqualityComparer = class(TInterfacedObject, IEqualityComparer<TMyKey>)
function Equals(const Left, Right: TMyKey): Boolean; reintroduce;
function GetHashCode(const Value: TMyKey): Integer; reintroduce;
end;
function TMyKeyEqualityComparer.Equals(const Left, Right: TMyKey): Boolean;
begin
if Left.Key < (Right.Key - 1E-9) then
Result := TRUE
else if Left.Key > (Right.Key + 1E-9) then
Result := FALSE
else if Left.Value < Right.Value then
Result := TRUE
else
Result := FALSE;
end;
function TMyKeyEqualityComparer.GetHashCode(const Value: TMyKey): Integer;
begin
Result := Value.Value + Round(Value.Key * 1E6);
end;
var
Events : IMultiMap<TMyKey, Integer>;
KeyComparer : IEqualityComparer<TMyKey>;
begin
KeyComparer := TMyKeyEqualityComparer.Create;
// Next line triggers error: "E2250 There is no overloaded version of 'Create' that can be called with these arguments"
Events := TMultiMap<TMyKey, Integer>.Create(KeyComparer);
end.
在 Spring4D 源代碼中,我找到了以下聲明:
TMultiMap<TKey, TValue> = class(TMultiMapBase<TKey, TValue>)
並且還聲明了 TMultiMap acestor:
TMultiMapBase<TKey, TValue> = class abstract(TMapBase<TKey, TValue>,
IMultiMap<TKey, TValue>, IReadOnlyMultiMap<TKey, TValue>)
並有這個構造函數:
constructor Create(const keyComparer: IEqualityComparer<TKey>); overload;
這是我要調用的構造函數。 據我了解,我的論點 KeyComparer 具有正確的類型。 但顯然編譯器不同意:-(
如何修復此代碼?
您使用不同的IEqualityComparer<T>
類型。
您示例中的IEqualityComparer<T>
來自Spring.Tests.Interception.Types.pas
。 TMultiMap<TKey, TValue>.Create
構造函數中使用的一個在System.Generics.Defaults.pas
中定義。
如果您將uses
部分更改為
uses
System.SysUtils,
Generics.Collections,
// Spring.Tests.Interception.Types,
Generics.Defaults,
Spring.Collections.MultiMaps,
Spring.Collections;
您的示例將編譯。
使用 Delphi 10.3 U3 和 Spring4D 1.2.2 測試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.