【问题标题】:Implementing an abstract function with access types in Ada在 Ada 中实现具有访问类型的抽象函数
【发布时间】:2012-03-30 15:51:47
【问题描述】:

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

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

到目前为止一切顺利。

但我不知道该怎么做是调用正确的执行方法。我现在只有一个关键字编码,因为它还没有工作。

对不起,如果我的描述听起来令人费解。

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

【问题讨论】:

    标签: pointers abstract-class abstract ada


    【解决方案1】:

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

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

    顺便说一下,作为风格问题,通常最好将调度参数放在第一位;然后你可以(在 Ada 2005 中)说

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

    【讨论】:

    • @deuteros - 在这种情况下,我建议您通过单击此答案旁边的复选标记的轮廓让每个人都知道(并顺便奖励 Wright 先生)。这将“接受”这个答案。
    • 刚刚做到了。我以前做不到,因为我的名声不够高。
    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多