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.
Your confusion appears to include both the use of packages and the way dispatching operations are defined in Ada. Dispatching operations must be define in the same package defining the tagged data type, but before any other types are defined.
The package specification defines the tagged type Token and its dispatching operation Root_Of_Tree. The record type Token contains one integer data element named X. The body of the package is:
I have used child packages to define the Otoken and Vtoken types.
The body of Tokens.OTokens is:
The Specification of Tokens.VTokens is:
The body Tokens.Vtokens is:
A main procedure to create an array containing one otoken and one vtoken is:
It is good to remember that the type OToken contains two fields, X and Y. The type VToken contains two fields X and Z. The output of the main procedure is: