2011-11-05 122 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

相關問題