2016-03-04 43 views
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))工作?

+1

我希望我錯了,但我不確定那個例子是否真的可用。你不能只用'Ada.Containers'嗎? (即使你被困在Ada 2005中,你也可以使用矢量來實現一個隊列)。 –

+0

Simon,謝謝你的回答!我的一項任務是製作磁盤的文件夾樹(具有acls列表和其他特性),這就是爲什麼我決定爲它創建自己的容器。所以我沒有進行類型轉換。我想知道,這個例子(package1)是否可用。我很難相信,寫在書中的這個例子是不可用的。 (抱歉我的英文不好) – George

+0

我在[comp.lang.ada]上發佈了這個問題(https://groups.google.com/forum/?hl=zh-CN-GB#!topic/comp.lang.ada/hDKe3oENZNw ),普遍的觀點是這本書是錯誤的。 –

回答

0

西蒙·懷特提供了註釋的答案:

我張貼在comp.lang.ada這個問題,普遍的看法是,這本書是錯誤的。

我會繼續我的工作與方法中的元素(根據我的問題package2)。