在Ada中实现具有访问类型的抽象函数

时间:2012-03-30 15:51:47

标签: pointers abstract-class abstract ada

我有一个名为Statements的包,其中包含一个名为Statement的抽象类型和一个名为execute()的抽象函数。在另一个包中,我有一个类型CompoundStatement,它是一个Statement类型,它实现了execute()函数。

我有一个名为createStatement()的函数。它的目的是评估Unbounded_String类型的标记并确定它包含的关键字。然后根据此关键字,它将根据此关键字生成访问类型。

到目前为止一切顺利。

但我无法弄清楚如何做是调用正确的execute方法。我现在只有一个关键字编码,因为它还没有工作。

很抱歉,如果我的描述听起来很复杂。

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

1 个答案:

答案 0 :(得分:4)

execute的第三个参数应该是Statement的(孩子),但是你给它的是指向 a(孩子的)的指针Statement。你可能想要

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

顺便说一下,通常最好将调度参数作为第一个;你可以(在Ada 2005中)说

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