Generics 一般记录的定义

Generics 一般记录的定义,generics,types,record,ada,Generics,Types,Record,Ada,我对一般记录的定义有一些问题: -- ADS File package Stack is -- (generic) Entry generic type StackEntry is private; -- An array of Entries (limited to 5 for testing) type StackEntryHolder is array (0..5) of StackEntry; -- Stack type containing

我对一般记录的定义有一些问题:

-- ADS File
package Stack is

    -- (generic) Entry
    generic type StackEntry is private;

    -- An array of Entries (limited to 5 for testing)
    type StackEntryHolder is array (0..5) of StackEntry;

    -- Stack type containing the entries, it's max. size and the current position
    type StatStack is
    record                                  -- 1 --
        maxSize : Integer := 5;             -- max size (see above)
        pos : Integer := 0;                 -- current position
        content : StackEntryHolder;         -- content entries
    end record;


    -- functions / procedures

end Stack;
如果我编译它,我会在-1-处得到以下错误:

泛型类型定义中不允许记录


我认为您需要创建一个提供私有类型StatStack及其操作的通用包。

我认为您需要创建一个提供私有类型StatStack及其操作的通用包。

我认为您正在寻找类似以下内容:

generic 
   type StackEntry is private;
package Stack_G is

   type ReturnCode is (Ok,Stack_Full,Stack_Empty);

   -- functions / procedures
   procedure Push (E  : in     StackEntry;
               RC :    out ReturnCode);
   procedure Pop (E  : out StackEntry;
              RC : out ReturnCode);
private
   -- An array of Entries (limited to 5 for testing)
   type StackIndex is new Integer range 1 .. 5;
   type StackEntryHolder is array (StackIndex) of StackEntry;

   -- Stack type containing the entries, it's max. size and the current position
   type StatStack is record 
      IsEmpty : Boolean := True;
      Pos : StackIndex := StackIndex'First;-- current position
      Content : StackEntryHolder;          -- content entries
   end record;

end Stack_G;
您不需要maxSize,您可以从array属性获得它 '长度或堆栈索引类型'last。 我已将堆栈重命名为堆栈\u g我的命名约定以指示 这是一个通用包 StackEntry是泛型的一个参数,您需要 这是在实例化堆栈包时发生的。 我添加了一个堆栈索引类型,在这里,我养成了 在Ada中使用新类型和子类型,可以节省数小时的时间 后来
我想你在寻找更像这样的东西:

generic 
   type StackEntry is private;
package Stack_G is

   type ReturnCode is (Ok,Stack_Full,Stack_Empty);

   -- functions / procedures
   procedure Push (E  : in     StackEntry;
               RC :    out ReturnCode);
   procedure Pop (E  : out StackEntry;
              RC : out ReturnCode);
private
   -- An array of Entries (limited to 5 for testing)
   type StackIndex is new Integer range 1 .. 5;
   type StackEntryHolder is array (StackIndex) of StackEntry;

   -- Stack type containing the entries, it's max. size and the current position
   type StatStack is record 
      IsEmpty : Boolean := True;
      Pos : StackIndex := StackIndex'First;-- current position
      Content : StackEntryHolder;          -- content entries
   end record;

end Stack_G;
您不需要maxSize,您可以从array属性获得它 '长度或堆栈索引类型'last。 我已将堆栈重命名为堆栈\u g我的命名约定以指示 这是一个通用包 StackEntry是泛型的一个参数,您需要 这是在实例化堆栈包时发生的。 我添加了一个堆栈索引类型,在这里,我养成了 在Ada中使用新类型和子类型,可以节省数小时的时间 后来
这是因为您编写的代码没有遵循泛型声明的正确语法。您可以在中查看其辉煌的BNF形式

基本上,您必须决定是要声明泛型包还是泛型例程。假设您想要的不仅仅是一个通用的子例程,我假设您想要一个包。考虑到它应该看起来像:

泛型{generic formal stuff}{package declaration}

…其中{package declaration}只是一个普通的包delcaration,但可能使用泛型形式部分中声明的内容,{generic formal stuff}是一系列泛型形式参数的delcaration,客户端将传递到泛型形式部分


在您的代码中发生的事情是,编译器看到了“generic”这个神奇的词,并且现在期望在下一个子程序或包delcaration将成为generic形式参数之前的所有内容。它发现的第一个,同一行上的私有类型delcaration,很好。但是,下一行包含一个完整的记录声明,它看起来根本不像一个通用的形式参数。因此编译器感到困惑并抛出了一个错误。

这是因为您编写的代码没有遵循泛型声明的正确语法。您可以在中查看其辉煌的BNF形式

基本上,您必须决定是要声明泛型包还是泛型例程。假设您想要的不仅仅是一个通用的子例程,我假设您想要一个包。考虑到它应该看起来像:

泛型{generic formal stuff}{package declaration}

…其中{package declaration}只是一个普通的包delcaration,但可能使用泛型形式部分中声明的内容,{generic formal stuff}是一系列泛型形式参数的delcaration,客户端将传递到泛型形式部分


在您的代码中发生的事情是,编译器看到了“generic”这个神奇的词,并且现在期望在下一个子程序或包delcaration将成为generic形式参数之前的所有内容。它发现的第一个,同一行上的私有类型delcaration,很好。但是,下一行包含一个完整的记录声明,它看起来根本不像一个通用的形式参数。因此,编译器感到困惑,并抛出了一个错误。

有关泛型包的示例,请参见的结尾。谢谢!是的,你说得对,我在这里需要一个通用软件包。有关通用软件包的示例,请参见的末尾。谢谢!是的,你说得对,我这里需要的是一个通用包。我会使用类型FullStackIndex范围0。。5并使StackIndex成为子类型范围1。。FullStackIndex'Last。这样你就可以不用我了。而且我也不会使用带有堆栈的内部名称。。我们已经知道这是关于堆栈的@西蒙赖特:我考虑过子类型的想法,但认为它超出了我已经提出的问题的范围!到底是什么解决了问题!谢谢你,现在我明白我做错了什么。仅出于兴趣:为什么您更喜欢ReturnCode的out参数而不是ReturnValue函数?可能是因为您可以用这种方式返回值和ReturnCode,或者是因为函数只能有in参数?@ollo yes和yes对您的第二条评论:我会使用类型FullStackIndex范围0。。5并使StackIndex成为子类型范围1。。FullStackIndex'Last。这样你就可以不用我了。而且我也不会使用带有堆栈的内部名称。。我们已经知道这是关于堆栈的@西蒙赖特我想到了这个小问题
我有一个想法,但决定它超出了我已经提出的问题的范围!到底是什么解决了问题!谢谢你,现在我明白我做错了什么。仅出于兴趣:为什么您更喜欢ReturnCode的out参数而不是ReturnValue函数?可能是因为您可以通过这种方式返回值和ReturnCode,或者是因为函数只能有in参数?@ollo yes和yes,请参见第二条注释: