DCC 错误...:E2010 不兼容的类型:'整数'和“整数”
尝试构建基于接口和泛型的图形并得到一个奇怪的错误 - 请注意错误行中“整数”一词的大小写差异。
文本解析器被传递给图形实现,然后由图形调用来构建其基本数据结构。可以构建更多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该行
描述了一个泛型类型,其中使用的两个泛型类型名称是 integer 和 TGlyph(例如 K 和 V位于
IGraph
中)。这些只是占位符,不应与现有类型integer
和TGlyph
混淆。The line
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 typesinteger
andTGlyph
.我假设如果
K
是整数,您想要实现一些特殊行为。这称为专业化,并且可以在 C++ 中实现(链接到MSDN 杂志文章涵盖了模板专业化),但不是在 Delphi 中。最好避免这种专门化,而只使用泛型类型K
(这应该很容易,否则泛型类一开始就没有多大意义)。如果您确实需要特殊情况,有一个解决方法:然后您可以比较类型信息(您需要为此包含单元
TypInfo
):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 typeK
(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
TypInfo
for this):