Gnat(ada95)使用我的软件包时出现错误“ ...不可见”错误

时间:2019-07-02 02:58:22

标签: ada

使用Gnat 7.4.0。

我是Ada菜鸟,对错误消息感到困惑:

$ gnat make list_test.adb
x86_64-linux-gnu-gcc-7 -c list_test.adb
list_test.adb:9:18: "List" is not visible (more references follow)
list_test.adb:9:18: non-visible declaration at linked_list.ads:19
x86_64-linux-gnu-gnatmake-7: "list_test.adb" compilation error

我不明白为什么我的List不可见。

我正在尝试编写一个带有通用负载的链表,以自学Ada。我用类似的布局(通用布局除外)编写了一个二叉树,但没有出现此错误。

.ads

with Ada.Unchecked_Deallocation;

generic
    type Payload_Type is private;

package Linked_List is

    type List_Node;
    type List_Node_Pointer is access all List_Node;

    type List_Node is
    record
        payload : Payload_Type;
        next    : List_Node_Pointer := null;
        prev    : List_Node_Pointer := null;
    end record;

    type List is
    record
        head  : List_Node_Pointer := null;
        tail  : List_Node_Pointer := null;
        count : Natural           := 0;
    end record;

    type List_Pointer is access all List;


    procedure pushTail( base    : in List;
                        payload : in Payload_Type );

    procedure pushHead( base    : in List;
                        payload : in Payload_Type );

    function popTail( base : List ) return Payload_Type;

    function contains( base    : List;
                       payload : Payload_Type ) return Boolean;


private

    procedure free is new Ada.Unchecked_Deallocation( List_Node, List_Node_Pointer );

end Linked_List;

还有.adb :(出于完整性考虑)

with Ada.Assertions;

package body Linked_List is

    procedure pushTail( base    : in out List;
                        payload : in Payload_Type ) is
    begin
        if ( base.head = null ) then
            -- list is empy
            base.head  := new List_Node;
            base.tail  := base.head;
            base.head.payload := payload;
        else
            -- list is not empty, add to the tail
            base.tail.next := new List_Node;
            base.tail.next.prev := base.tail;
            base.tail := base.tail.next;
            base.tail.payload := payload;
        end if;
        base.count := base.count + 1;
    end push;


    ...  -- More definitions for pushHead(), popTail(), contains()

private

end Linked_List;

list_test.adb类似于:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Linked_List;

procedure List_Test is
    my_list    : List;
    found      : Boolean;
begin
    List.pushTail( my_list, Ada.Strings.Unbounded.To_Unbounded_String( "First" ) );
    List.pushTail( my_list, Ada.Strings.Unbounded.To_Unbounded_String( "2nd" ) );

    ... etc.

最初我的语法是:

my_list : Linked_List.List;

但是纳特不喜欢它-我也不明白为什么也不接受。

编辑:我也尝试过

procedure List_Test is
    package List_String is new List( Ada.Strings.Unbounded.Unbounded_String );

    my_list    : List_String;

但是我遇到同样的"List" is not visible错误。

EDIT2:

with Linked_List;

procedure List_Test is
    package String_List is new List( Ada.Strings.Unbounded.Unbounded_String ); use String_List;

    my_list    : String_List;

仍然产生错误:

list_test.adb:10:32: "List" is not visible
list_test.adb:10:32: non-visible declaration at linked_list.ads:10
list_test.adb:10:89: "String_List" is undefined (more references follow)

1 个答案:

答案 0 :(得分:6)

由于Linked_List包是通用的,因此您不能直接引用类型Linked_List.List。您只能在实例化包中引用List类型。因此,请尝试在实例化软件包use List_String;实例化后直接插入Linked_List,或使用List_String.List引用List软件包中的List_String类型:

procedure List_Test is

   package List_String is 
      new Linked_List (Ada.Strings.Unbounded.Unbounded_String);
   use List_String;

   my_list : List

procedure List_Test is

   package List_String is 
      new Linked_List (Ada.Strings.Unbounded.Unbounded_String);

   my_list : List_String.List