1

我正在处理这里的示例:https ://www.adahome.com/rm95/rm9x-12-08.html

我写了我的generic_stack.ads

generic
    type Item_Type is private;
    size : Positive;

package Generic_Stack is
    procedure push( item : in Item_Type );

    function  pop return Item_Type;

    function  is_Empty return Boolean;

    STACK_EMPTY : exception;
    STACK_FULL  : exception;

end Generic_Stack;

我的generic_stack.adb

package body Generic_Stack
is
    type Stack_Table is array (Positive range <>) of Item_Type;
    nodes : Stack_Table( 1..size );
    index : Natural := 0;

    procedure push( item : in Item_Type )
    is
    begin
        if ( index < size )
        then
            index := index + 1;
            nodes(index) := item;
        else
            raise STACK_FULL;
        end if;
    end push;

    function pop()
    return
        Item_Type
    is
        item : Item_Type;
    begin
        if ( index > 0 )
        then
            item := nodes( index );
            index := index - 1;
        else
            raise STACK_EMPTY;
        end if;
        return item;
    end pop;

    -- function is_Empty()   removed for the sake of brevity
end Generic_Stack;

我真的不明白如何实际使用Generic_Stack.
使用简单的generic_stack_test.adb 代码:

with Generic_Stack;
package Stack_Int_Type is new Generic_Stack( Item_Type => Integer, Size => 32 );

procedure Generic_Stack_Test
is
    stack : Stack_Int_Type;
begin
    stack.push( 3 );
end Generic_Stack_Test;

Gnat 给我编译错误:

# gnat make -gnat95 generic_stack_test.adb -o generic_stack_test
x86_64-linux-gnu-gcc-8 -c -gnat95 generic_stack_test.adb
generic_stack_test.adb:9:08: keyword "body" expected here [see file name]
generic_stack_test.adb:20:24: missing "end Stack_Int_Type;"
x86_64-linux-gnu-gnatmake-8: "generic_stack_test.adb" compilation error

我必须这样做declareStack_Int_Type?我不明白如何在过程中使用声明。如果我将 a 传递Stack_Int_Type给另一个过程,它是否也必须声明类型?

是否可以简单地在 中声明Stack_Int_Type 一次.ads,并将其用作常规类型?我的书和网页有点建议每次都必须声明它,这听起来很麻烦。

4

3 回答 3

3

您的测试代码实际上是两个库项:

with Generic_Stack;
package Stack_Int_Type is new Generic_Stack( Item_Type => Integer, Size => 32 );

声明一个库包Stack_Int_Type,并且

procedure Generic_Stack_Test
is
    stack : Stack_Int_Type;
begin
    stack.push( 3 );
end Generic_Stack_Test;

声明了一个库过程,就目前而言,它对Stack_Int_Type.
我们可以通过添加必要的with, 但是(编译-gnatl)来解决这个问题

 1. with Stack_Int_Type;
 2. procedure Generic_Stack_Test
 3. is
 4.    stack : Stack_Int_Type;
               |
    >>> subtype mark required in this context
    >>> found "Stack_Int_Type" declared at stack_int_type.ads:2

 5. begin
 6.    stack.push( 3 );
       1    2
    >>> invalid prefix in selected component "stack"
    >>> prefixed call is only allowed for objects of a tagged type

 7. end Generic_Stack_Test;

这里发生的是它Generic_Stack没有声明类型,所以你不能在第 4 行声明它的实例;这是一种单例。(除其他外,这意味着它的名称令人困惑:我会调用它Integer_Stack。永远不要调用包_Type; _Types,也许。)解决这个问题,

with Generic_Stack;
package Integer_Stack is new Generic_Stack( Item_Type => Integer, Size => 32 );

with Integer_Stack;
procedure Generic_Stack_Test
is
begin
   Integer_Stack.push( 3 );
end Generic_Stack_Test;

你可以Integer_Stack本地化:

with Generic_Stack;
procedure Generic_Stack_Test
is
   package Integer_Stack
   is new Generic_Stack( Item_Type => Integer, Size => 32 );
begin
   Integer_Stack.push( 3 );
end Generic_Stack_Test;
于 2020-04-22T07:45:04.070 回答
2

您的 Generic_Stack 包从不定义堆栈数据类型。Push 和 Pop 过程是对堆栈的操作。您必须有一个可以操作的类型。堆栈有两种一般类别;有界堆栈和无界堆栈。您必须决定要创建哪种堆栈。关于在 Ada 中实现的两种堆栈的讨论可以在https://sworthodoxy.blogspot.com/2019/02/stack-abstract-data-type-using-ada.html找到

在上面 URL 中引用的示例中,您将看到如何创建可以多次使用的堆栈类型。Adahome 的例子是一个古老且有问题的例子。你发现了最大的问题。

于 2020-04-22T01:25:59.177 回答
1

正如其他地方所解释的,您的包规范没有定义任何类型。否则,您将在关键字type ... is之后的某个位置。package就是这么简单。

但正如其他地方所解释的那样,这并不引人注目。您的包实例化将只定义一个堆栈,而不是在多个地方使用的类型。在某些情况下,这正是您所需要的。所以,你可以调用你的实例化包My_Stack,它实际上围绕一个对象(My_Stack.nodes,只能通过Push&访问Pop)。

您需要Generic_Stack从一个单元(过程,包,...)中进行实例化。在它之外,它是“外层空间”,只有与其他单元连接所需的with和子句。use

with Generic_Stack;

procedure Generic_Stack_Test
is
    package My_Stack is new Generic_Stack( Item_Type => Integer, Size => 32 );
begin
    My_Stack.push( 3 );
end Generic_Stack_Test;
于 2020-04-22T10:34:42.350 回答