繁体   English   中英

DCC错误…:E2010不兼容的类型:'integer'和'Integer'

[英]DCC Error …: E2010 Incompatible types: 'integer' and 'Integer'

尝试构建基于接口和基于泛型的图并得到一个奇怪的错误-请注意错误行中“整数”一词的大小写差异。

文本解析器传递给Graph实现,然后由Graph调用以构建其基本数据结构。 进一步的IGraphConstructor对象可以构建更复杂的实际图形,而不仅仅是填充基本字典。

IGraphConstructor<K,V> = interface
  function Construct(AData : TObjectDictionary<K,V>) : boolean;
end;

IGraph<K,V> = interface
  ['{B25EEE1F-3C85-43BB-A56B-3E14F7EA926C}']
  function Construct(AConstructor : IGraphConstructor<K,V>) : boolean;
  function GetNodes : TObjectDictionary<K,V>;
  property Nodes : TObjectDictionary<K,V> read GetNodes;
end;

TGraph<K,V> = class(TComponent, IGraph<K,V>)
private
  FData : TObjectDictionary<K,V>;
  function GetNodes : TObjectDictionary<K,V>;
... 

//the editor
TVirtualEditor = class(TComponent)
private
  FGlyphs : TGraph<integer,TGlyph>;
...  

TTextParser<integer,TGlyph> = class(TInterfacedObject, IGraphConstructor<integer,TGlyph>)
... 

和...

function TVirtualEditor.Edit(AText: string): boolean;
var
  parser : TTextParser<integer,TGlyph>;
begin
  parser := TTextParser<integer,TGlyph>.Create(AText);
  result := FGlyphs.Construct(parser);
end;

function TTextParser<integer,TGlyph>.Construct(AData: TObjectDictionary<integer,TGlyph>): boolean;
var
  i : integer;
begin
  for i := 1 to length(FText) do
  begin
    //#1
    AData.AddOrSetValue(i, TGlyph(TCharGlyph.Create( FText[i] )) ); //!--> error [DCC Error] ...: E2010 Incompatible types: 'integer' and 'Integer'
  end;

  //uc....

end;

TTextParser<K,V>声明为TTextParser<K,V>并将其用作

TParser : TTextParser<integer,TGlyph>;

返回并在#1处出错

[DCC Error] ...: E2010 Incompatible types: 'K' and 'Integer'

编辑:解决方法

找到了一种解决方法,但不确定这样做的方法。

function TTextParser<K,V>.Construct(AData: TObjectDictionary<K,V>): boolean;
var
  i : integer;
  n : K;
  o : V;
begin
  for i := 1 to length(FText) do
  begin
    n := K((@i)^);
    o := V(TCharGlyph.Create( FText[i] ));
    AData.AddOrSetValue(n, o );
  end;
  result := true;
end;

线

TTextParser<integer,TGlyph> = class(TInterfacedObject, IGraphConstructor<integer,TGlyph>)

描述了一个通用类型,其中使用的两个通用类型名称分别是integerTGlyph (例如IGraph<K,V> KV )。 这些只是占位符,不应与现有类型integerTGlyph

我假设如果K为整数,则希望实现某些特殊行为。 这称为专业化 ,可以在C ++中实现( 链接到MSDN杂志的文章,其中涉及模板专业化 ),但在Delphi中则不行。 最好避免这样的专业化,而只使用通用类型K (这应该很容易,否则,通用类一开始就没有多大意义)。

如果确实需要特殊情况,有一个解决方法:然后可以比较类型信息(为此需要包含单元TypInfo ):

if (TypeInfo(K) = TypeInfo(Integer)) then
  begin
  // special case
  end;

暂无
暂无

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

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