DCC 错误...:E2010 不兼容的类型:'整数'和“整数”

发布于 2024-12-14 00:27:39 字数 2190 浏览 0 评论 0原文

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

文本解析器被传递给图形实现,然后由图形调用来构建其基本数据结构。可以构建更多 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 声明为 TTextParser 并将其用作

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;

Attempting to build an interface and generics based graph and getting an odd error - note the case difference in the word 'integer' in the error line.

The text parser is passed to a Graph implementation and then called by the Graph to build its base data structure. Further IGraphConstructor objects can build
more complex actual graphs and not just populate the base dictionary.

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>)
... 

and...

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;

Declaring TTextParser as TTextParser<K,V> and using it as

TParser : TTextParser<integer,TGlyph>;

returns and error at #1 of

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

EDIT: Workaround

Found a workaround but not sure that's the way to do it.

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清泪尽 2024-12-21 00:27:39

该行

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

描述了一个泛型类型,其中使用的两个泛型类型名称是 integerTGlyph(例如 KV位于 IGraph 中)。这些只是占位符,不应与现有类型 integerTGlyph 混淆。

The line

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

describes a generic type where the two used generic type names are integer and TGlyph (like K and V are in IGraph<K,V>). Those are only placeholders and should not be confused with the existing types integer and TGlyph.

ぶ宁プ宁ぶ 2024-12-21 00:27:39

我假设如果 K 是整数,您想要实现一些特殊行为。这称为专业化,并且可以在 C++ 中实现(链接到MSDN 杂志文章涵盖了模板专业化),但不是在 Delphi 中。最好避免这种专门化,而只使用泛型类型 K (这应该很容易,否则泛型类一开始就没有多大意义)。

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

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

I assume that you wanted to achieve some special behaviour if K is integer. This is called specialization and is possible in C++ (link to MSDN magazine article covering template specialization), but not in Delphi. It is best to avoid such specializations and just work with the generic type K (this should be easy, otherwise the generic class does not make much sense in the first place).

There is a workaround if you really need a special case: you can then compare the type infos (you need to include unit TypInfofor this):

if (TypeInfo(K) = TypeInfo(Integer)) then
  begin
  // special case
  end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文