我正在尝试熟悉 Ada 中的面向对象。几个月前,您的网站帮助我解决了另一个 OO 问题,我希望您愿意再次提供帮助。
情况:我有一个抽象类型“token”和两个派生类型“otoken”和“vtoken”。我想将 2 个派生类型放在同一个数组中并让它们正确调度。
我的教科书建议将数组声明为包含指向 token'class 的指针,这迫使我从头到尾处理点。下面是我的程序的精简版本,但它不会编译,因为编译器说我的调度调用是“模棱两可的”</p>
---------------------------------------------------------------------------------
--------------------------------------------
-- Tokensamp.ads
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
package tokensamp is
type token is abstract tagged record
x: integer;
end record;
type otoken is new token with record
y: integer;
end record;
type vtoken is new token with record
z: integer;
end record;
type potoken is access otoken;
type pvtoken is access vtoken;
end tokensamp;
------------------------------------------------------------------------------------------------------
-- Parsesamp.ads:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with tokensamp;
package parsesamp is
function rootOfTree( t: tokensamp.pvtoken) return integer;
function rootOfTree( t: tokensamp.potoken) return integer;
end parsesamp;
-------------------------------------------
-- parsesamp.adb:
package body parsesamp is
function rootOfTree( t: tokensamp.pvtoken) return integer is
begin
return t.z * 2;
end rootOfTree;
function rootOfTree( t: tokensamp.potoken) return integer is
begin
return t.y * 2;
end rootOfTree;
result: integer;
type tarray is array (1..2) of access tokensamp.token'class ;
tl: tarray;
begin
for i in 1..2 loop
result := rootOfTree( tl(i) );
end loop;
end parsesamp;
-------------------------------------------------------------
当我用我的 GNAT Ada 95 编译器编译它时,我收到错误消息:
C:\GNAT\2018\bin\ceblang>gnatmake parsesamp.adb
gcc -c parsesamp.adb
parsesamp.adb:25:27: ambiguous expression (cannot resolve "rootOfTree")
parsesamp.adb:25:27: possible interpretation at parsesamp.ads:9
parsesamp.adb:25:27: possible interpretation at parsesamp.ads:8
gnatmake: "parsesamp.adb" compilation error
换句话说,它无法将这两个函数识别为替代调度调用。如果你能建议我,我将不胜感激,因为我已经坚持了好几天了。