Pointers 在Ada中基于关键字创建对象并调用其方法
这是我问题的后续部分 我在程序中到了一个地步,我觉得我无法继续当前的结构,所以我做了很多重写。Pointers 在Ada中基于关键字创建对象并调用其方法,pointers,ada,Pointers,Ada,这是我问题的后续部分 我在程序中到了一个地步,我觉得我无法继续当前的结构,所以我做了很多重写。语句类型不再是抽象的,语句的每个子类型都创建自己的语句变量实例。我还从Statements包中删除了abstractexecute函数,因为编译器不喜欢这样(不管怎样,每个子类型都将有自己的execute方法)。execute函数已更改为过程,因为必须修改传入语句类型。我将statementFactory(以前的createStatement)移动到语句包中 下面是我得到的错误: statements-
语句
类型不再是抽象的,语句
的每个子类型都创建自己的语句
变量实例。我还从Statements包中删除了abstractexecute
函数,因为编译器不喜欢这样(不管怎样,每个子类型都将有自己的execute
方法)。execute
函数已更改为过程,因为必须修改传入语句类型。我将statementFactory
(以前的createStatement)移动到语句
包中
下面是我得到的错误:
statements-compoundstatements.adb:15:29: expected type "CompoundStatement" defined at statements-compoundstatements.ads:11
statements-compoundstatements.adb:15:29: found type "Statement'Class" defined at statements.ads:6
我是Ada的初学者,但我的直觉是,因为execute
过程位于CompoundStatements
(这是语句的“子类”)中,所以它永远无法看到另一个语句“子类”的execute
方法。我能想到的唯一解决方案是将调用execute
过程的所有execute
过程转储到语句
包中,但这似乎是不可取的。但这仍然不能解释为什么stmt.all
被用作类型Statement'Class
,而不是在statementFactory
中创建的类型
以下是新代码:
package Statements is
type Statement is tagged private;
type Statement_Access is access all Statement'Class;
ParserException : Exception;
procedure createStatement(tokens : Vector; S : out Statement);
procedure statementFactory(S: in out Statement; stmt: out Statement_Access);
--.....A bunch of other procedures and functions.....
private
type Statement is tagged
record
tokens : Vector;
executedtokens : Vector;
end record;
end Statements;
您说“我还从语句包中删除了抽象执行函数,因为编译器不喜欢它”;但是您确实需要它,因为否则编译器怎么知道当您调用
execute(skip,stmt.all,tokensexecuted)
时,任何stmt.all
都将提供一个execute
,以便将其分派到
扩展类型不仅继承其父类型的属性(因此每个CompoundStatement
etc已经有tokens
和executedtokes
),它还继承父类型的基本操作;如果父操作是抽象的,则子操作必须提供自己的实现,否则,子操作可以提供自己的(重写)实现
有关这方面的详细讨论,请参阅和。好的,我在
中添加了返回的过程execute(skip:in Boolean;S:in out语句;reset:out Integer)是抽象的但是编译器仍然抱怨语句
不是抽象的,但是我之前已经做了抽象,编译器也不喜欢这样。原来我也忘了把语句的记录也抽象化了。这似乎解决了问题。花几个小时在一个问题上,结果证明它是非常简单的。再次感谢你。我已经花了几个星期的时间在这个项目上,它将在周一到期,但即使我没有让它完全工作,我觉得我已经学到了很多关于Ada和编码的知识,我以前不知道这些。此外,我对Ada的恨也少了一点,因为我对它的理解好多了。:)
procedure createStatement(tokens : Vector; S : out Statement) is
begin
S.tokens := tokens;
end createStatement;
procedure statementFactory(S: in out Statement; stmt: out Statement_Access) is
currenttoken : Unbounded_String;
C : CompoundStatement;
A : AssignmentStatement;
P : PrintStatement;
begin
currenttoken := getCurrentToken(S);
if currenttoken = "begin" then
createStatement(S.tokens, C);
stmt := new CompoundStatement;
stmt.all := Statement'Class(C);
elsif isVariable(To_String(currenttoken)) then
createStatement(S.tokens, A);
stmt := new AssignmentStatement;
stmt.all := Statement'Class(A);
elsif currenttoken = "print" then
createStatement(S.tokens, P);
stmt := new PrintStatement;
stmt.all := Statement'Class(P);
end statementFactory;
package body Statements.CompoundStatements is
procedure execute(skip: in Boolean; C: in out CompoundStatement; reset: out Integer) is
stmt: Statement_Access;
tokensexecuted: Integer;
currenttoken : Unbounded_String;
begin
match(C, "begin");
currenttoken := getCurrentToken(C);
while(currenttoken /= "end") loop
statementFactory(C, stmt);
execute(skip, stmt.all, tokensexecuted); //ERROR OCCURS HERE