在 Ada 中实现具有访问类型的抽象函数 [英] Implementing an abstract function with access types in Ada
问题描述
我有一个名为 Statements 的包,其中包含一个名为 Statement 的抽象类型和一个名为 execute() 的抽象函数.在另一个包中,我有一个 CompoundStatement 类型,它是一个 Statement 类型,它实现了 execute() 函数.
I have a package called Statements with an abstract type called Statement and an abstract function called execute(). In another package I have a type CompoundStatement which is a type Statement and it implements the execute() function.
我有一个名为 createStatement() 的函数.它的目的是评估 Unbounded_String 类型的标记并确定它包含的关键字.然后根据这个关键字生成一个基于这个关键字的访问类型.
I have a function called createStatement(). It's purpose is to evaluate a token of type Unbounded_String and determine what keyword it contains. Then based on this keyword it will generate an access type based on this keyword.
到目前为止一切顺利.
但是我不知道该怎么做是调用正确的执行方法.我现在只有一个关键字编码,因为它还没有工作.
But what I can't figure out how to do is call the correct execute method. I only have one keyword coded in right now because it's not working yet.
对不起,如果我的描述听起来很复杂.
Sorry if my description sounds convoluted.
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;
我在编译时遇到这个错误:
I get this error when I compile:
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
推荐答案
execute
的第三个参数应该是 Statement
的(子代),但是你'已经给了它一个指向Statement
的(子代)的指针.你可能想要
The third parameter to execute
is expected to be a (child of) Statement
, but what you’ve given it is a pointer to a (child of) Statement
. You probably want
tokensexecuted := execute(skip, TK, stmt.all);
顺便说一句,通常最好将调度参数放在第一位;然后你可以(在 Ada 2005 中)说
As a matter of style, by the way, it’s usually best to make the dispatching parameter the first; you could then (in Ada 2005) say
tokensexecuted := stmt.execute(skip, TK);
这篇关于在 Ada 中实现具有访问类型的抽象函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!