我想实现“Ada Distilled”(2005)的队列管理器示例,以适应我的工作。最少的代码:
package1.ads:
with Ada.Finalization;
use Ada;
generic
type Element is tagged private;
package Lists is
use Ada.Finalization;
type Item is new Element with private;
type Item_Reference is access all Item'Class;
type Forward_List is new Controlled with private;
function Init return Forward_List;
function Is_Empty(Self:Forward_List'Class) return Boolean;
procedure Add(Self:in out Forward_List;I:in Item'Class);
private
type Item is new Element with null record;
type Forward_List is new Controlled with
record
.........
end record;
type Forward_List_Reference is access all Forward_List;
overriding procedure Initialize(List:in out Forward_List);
overriding procedure Finalize(List:in out Forward_List);
end Lists;
包体:
package body Lists is
function Init return Forward_List is
FL_new:Forward_List_Reference:=new Forward_List;
begin
return FL_new.all;
end Init;
function Is_Empty(Self:Forward_List'Class) return Boolean is
begin
return Self.Count=0;
end Is_Empty;
procedure Add(Self:in out Forward_List;I:Item'Class) is
begin
null;
end Add;
procedure Finalize(Node:in out Forward_List_Node) is
begin
null;
end Finalize;
overriding procedure Initialize (List:in out Forward_List) is
begin
null;
end Initialize;
overriding procedure Finalize (List:in out Forward_List) is
begin
null;
end Finalize;
end Lists;
Main.adb(使用此包):
with Lists;
with GNAT.Strings;
with Ada.Finalization;use Ada.Finalization;
procedure main is
use GNAT.Strings;
type Temp is new Controlled with
record
Name:Integer;
end record;
package Temp_List is new Lists(Temp);
FL:Temp_List.Forward_List:=Temp_List.Init;
Instance:Temp:=Temp'(Controlled with
Name => 60);
begin
Temp_List.Add(Self => FL,
I => Instance);
end main;
结果:
Builder results
E:\ADA\test_containers\20160303\main.adb
15:11 expected an access type with designated type "Item'class" defined at lists.ads:10, instance at line 9
found type "Temp"
我想整个星期解决这个问题,阅读有关此问题的文献(包,标记记录,类范围类型和泛型),并且无法理解这个问题。
为了使这段代码工作,我修改了(将Element'Class by Element替换)包规范文件: package2.ads:
with Ada.Finalization;
use Ada;
generic
type Element is private;
package Lists is
use Ada.Finalization;
type Forward_List is new Controlled with private;
function Init return Forward_List;
function Is_Empty(Self:Forward_List'Class) return Boolean;
procedure Add(Self:in out Forward_List;I:in Element);
private
type E_Reference is access all Element;
type Forward_List is new Controlled with
record
.........
end record;
type Forward_List_Reference is access all Forward_List;
overriding procedure Initialize(List:in out Forward_List);
overriding procedure Finalize(List:in out Forward_List);
end Lists;
然后代码“main.adb”开始很好。
这就是我的问题:我如何使用第一个规范(例如“Ada Distilled”(package1))?
答案 0 :(得分:0)
Simon Wright在评论中提供了答案:
我在comp.lang.ada上发布了这个问题,一般认为这本书是错误的。
我将继续使用Element in methods(根据我的问题中的package2)。