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

Kin*_*ley 0 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
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我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;
Run Code Online (Sandbox Code Playgroud)

.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;
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

最初我有以下语法:

my_list : Linked_List.List;
Run Code Online (Sandbox Code Playgroud)

但是Gnat不喜欢它-我不明白为什么这也不被接受。

编辑:我也尝试过

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

    my_list    : List_String;
Run Code Online (Sandbox Code Playgroud)

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

编辑2:

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;
Run Code Online (Sandbox Code Playgroud)

仍然会产生错误:

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)
Run Code Online (Sandbox Code Playgroud)

Dee*_*Dee 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
Run Code Online (Sandbox Code Playgroud)

要么

procedure List_Test is

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

   my_list : List_String.List
Run Code Online (Sandbox Code Playgroud)