1
我想从“阿达精华”(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"
我想整个星期都解决这个问题,阅读有关这方面的文献(包,标记记录,类范围类型和泛型),并不能理解问题。
为了让这段代码的工作我修改(由元素替换Item'Class)封装规格文件: 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” 开头良好。
所以这是我的问题:我怎么能与第一规格(例如,从“阿达精华”(包1))工作?
我希望我错了,但我不确定那个例子是否真的可用。你不能只用'Ada.Containers'吗? (即使你被困在Ada 2005中,你也可以使用矢量来实现一个队列)。 –
Simon,谢谢你的回答!我的一项任务是制作磁盘的文件夹树(具有acls列表和其他特性),这就是为什么我决定为它创建自己的容器。所以我没有进行类型转换。我想知道,这个例子(package1)是否可用。我很难相信,写在书中的这个例子是不可用的。 (抱歉我的英文不好) – George
我在[comp.lang.ada]上发布了这个问题(https://groups.google.com/forum/?hl=zh-CN-GB#!topic/comp.lang.ada/hDKe3oENZNw ),普遍的观点是这本书是错误的。 –