2011-11-05 85 views
1

试图建立一个接口和泛型基于图形和得到一个奇怪的错误 - 注意在错误行字“整数”的情况不同。DCC错误...:E2010不兼容的类型:“整”和“整数”

将文本解析器传递给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为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; 

回答

5

线

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

描述了一种通用的类型,其中两个使用的通用的类型名称整数TGlyph(如ķVIGraph<K,V>)。这些只是占位符,不应与现有类型integerTGlyph混淆。

1

我假设你想实现一些特殊的行为,如果K是整数。这被称为专业化并可能在C++(link to MSDN magazine article covering template specialization),但不是在德尔福。最好避免这种特殊化,只适用于通用类型K(这应该很容易,否则通用类首先没有多大意义)。

有一个解决办法,如果你真的需要一个特殊情况:那么你可以比较类型的相关信息(您需要包括本单位TypInfo):

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

正确 - 我改变,因为该方法 – MX4399

相关问题