the ads:
package Airlines_Package is
type Airlines( Size: Positive ) is limited private;
procedure Init( P: out Airlines );
procedure AssignName( P: out Airlines; Index: in Positive; Name: in String );
procedure InsertFlight( P: out Airlines; From, To: in String );
private
type Matrix is array ( Integer range <>, Integer range <>) of Boolean;
type NamesArray is array ( Integer range <> ) of String(1..10);
type Airlines( Size: Positive ) is record
Names: NamesArray( 1..Size );
Flights: Matrix( 1..Size, 1..Size );
end record;
end Airlines_Package;
the adb:
package body Airlines_Package is
procedure Init( P: out Airlines ) is
begin
for I in 1..P.Size loop
for J in 1..P.Size loop
if I = J then
P.Flights(I, J) := True;
else
P.Flights(I, J) := False;
end if;
end loop;
end loop;
end;
procedure AssignName( P: out Airlines; Index: in Positive; Name: in String ) is
begin
for I in 1..10 loop
P.Names(1)(I) := ' ';
end loop;
end;
procedure InsertFlight( P: out Airlines; From, To: in String ) is
begin
declare
FromIndex, ToIndex: Integer := -1;
begin
for I in 1..3 loop
if P.Names(I) = From then
FromIndex := I;
elsif P.Names(I) = To then
ToIndex := I;
end if;
end loop;
P.Flights(FromIndex, ToIndex) := True;
P.Flights(ToIndex, FromIndex) := True;
end;
end;
end Airlines_Package;
and the test program (main.adb):
with Ada.Integer_Text_IO, Airlines_Package;
use Ada.Integer_Text_IO, Airlines_Package;
procedure Main is
Q: Airlines(3);
begin
Init( Q );
AssignName( Q, 1, "Wien" );
AssignName( Q, 2, "London" );
AssignName( Q, 3, "Prague" );
InsertFlight( Q, "Prague", "London" );
end Main;
the error:
nagyz@platoon:~/ada$ gnatmake main
gcc-4.1 -c main.adb
gcc-4.1 -c airlines_package.adb
airlines_package.adb:27:20: warning: "P.Names" may be referenced before it has a value
gnatbind -x main.ali
gnatlink main.ali
nagyz@platoon:~/ada$ ./main
raised CONSTRAINT_ERROR : airlines_package.adb:33 index check failed
nagyz@platoon:~/ada$