2012-03-30 67 views
2

我有一個名爲Statements的包,其中包含一個名爲Statement的抽象類型和一個名爲execute()的抽象函數。在另一個包中,我有一個類型爲CompoundStatement的類型爲Statement的實現execute()函數。我有一個名爲createStatement()的函數。它的目的是評估Unbounded_String類型的標記並確定它包含的關鍵字。然後基於這個關鍵字,它會根據這個關鍵字生成一個訪問類型。在Ada中使用訪問類型實現抽象函數

到目前爲止這麼好。

但我不知道如何做是調用正確的執行方法。我現在只有一個關鍵字編碼,因爲它還沒有工作。

對不起,如果我的描述聽起來很複雜。

package Statements is 

    type Statement is abstract tagged private; 
    type Statement_Access is access all Statement'Class; 

    function execute(skip: in Boolean; T: in TokenHandler; S: in Statement) return Integer is abstract; 

private 
    type Statement is abstract tagged 
     record 
     tokens: Vector; 
     end record; 

end Statements; 

procedure createStatement(T : in TokenHandler; stmt: out Statement_Access) is 
     currenttoken : Unbounded_String; 
     C   : CompoundStatement; 

    begin 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(T)); 
     if currenttoken = "begin" then 
     createCompoundStatement(T, C); 
     stmt := new CompoundStatement; 
     stmt.all := Statement'Class(C); 
     end if; 
    end createStatement; 

    procedure createCompoundStatement(T : in TokenHandler; C: out CompoundStatement) is 
    begin 
     C.tokens := T.tokens; 
    end createCompoundStatement; 

function execute(skip: in Boolean; T: in TokenHandler; C: in CompoundStatement) return Integer is 
     TK: TokenHandler := T; 
     stmt: Statement_Access; 
     tokensexecuted: Integer; 
     currenttoken : Unbounded_String; 
    begin 
     TokenHandlers.match("begin", TK); 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(TK)); 
     while(currenttoken /= "end") loop 
     Put(To_String(currenttoken)); 
     createStatement(T, stmt); 
     tokensexecuted := execute(skip, TK, stmt); //ERROR OCCURS HERE 
     TokenHandlers.moveAhead(tokensexecuted, TK); 
     currenttoken := To_Unbounded_String(TokenHandlers.getCurrentToken(TK)); 
     end loop; 
     TokenHandlers.match("end", TK); 
     return TokenHandlers.resetTokens(TK);  
    end execute; 

我編譯時出現此錯誤:

statements-statementhandlers.adb:35:28: no candidate interpretations match the actuals: 
statements-statementhandlers.adb:35:46: expected type "CompoundStatement" defined at statements-statementhandlers.ads:14 
statements-statementhandlers.adb:35:46: found type "Statement_Access" defined at statements.ads:6 
statements-statementhandlers.adb:35:46: ==> in call to "execute" at statements-statementhandlers.ads:10 
statements-statementhandlers.adb:35:46: ==> in call to "execute" at statements.ads:8 

回答

4

execute的第三個參數預計Statement一(兒童),但你給它的是a指向(孩子)Statement。你可能想

tokensexecuted := execute(skip, TK, stmt.all); 

作爲一個風格的問題,順便說一下,通常是最好的調度參數的第一個;然後你可以(在2005年ADA)說

tokensexecuted := stmt.execute(skip, TK); 
+0

參見[* 2.3前綴符號*](http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-2- 3.html)。 – trashgod 2012-03-30 21:22:46

+0

太棒了!這工作。謝謝,威爾先生。 – 2012-03-30 22:29:06

+0

@deuteros - 在這種情況下,我建議你通過點擊該答案旁邊的複選標記輪廓,讓每個人都知道(並順帶獎勵賴特先生)。這將「接受」這個答案。 – 2012-04-03 16:12:23