I am trying to familiarize myself with object orientation In Ada. Your site helped me with another O-O problem a couple of months ago and I hope that you will be willing to help again.
The situation: I have an abstract type “token” and 2 derived types “otoken” and “vtoken”. I want to put the 2 derived types in the same array and get them to dispatch properly.
My textbook recommended declaring the array as containing pointers to token’class, which forces me to work through points throughout. A stripped-down version of my program is below, but it won’t compile because the compiler says my dispatch calls are “ambiguous”
---------------------------------------------------------------------------------
--------------------------------------------
-- 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;
-------------------------------------------------------------
When I compile this with my GNAT Ada 95 compiler, I get the error messages:
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
In other words, it’s failing to recognize the two functions as alternative dispatched calls. I’d be grateful if you could advise me as I’ve been stuck on this for several days.