简体   繁体   中英

Definition of a generic record

I have some trouble with the definition of a generic record:

-- 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;

If i compile this i get the following error (at -- 1 -- ):

record not allowed in generic type definition

我认为您想制作一个提供私有类型StatStack及其操作的通用

I think you are looking for something more like this:

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;
  1. You dont need a maxSize, you can get that from the array attribute 'length or the stack index type'last.
  2. I have renamed stack to be stack_g (my naming convention to indicate it is a generic package)
  3. The StackEntry is a parameter to the generic, you will need to us this when instantiating your stack package.
  4. Ive added a stack index type, where practical get in the habit of using new types and subtypes in Ada, it saves you hours of time later.

That's because the code you wrote doesn't follow the proper syntax for a generic declaration. You can view this in its glorious BNF form in the LRM .

Basically, you have to decide if you want to declare a generic package or a generic routine . Guessing you want more that just a single subroutine generic, I'll assume you want a package. Given that it should look something like:

generic {generic formal stuff} {package declaration}

...where "{package declaration}" is just a normal package delcaration (but one that may use stuff declared in the generic formal part), and "{generic formal stuff}" is a series of delcarations of generic "formal" parameters that the client will pass into the generic.

What happened in your code is that the compiler sees the magic word generic and is now expecting everything up until the next subprogram or package delcaration will be generic formal parameters. The first one it finds, the private type delcaration on the same line, is just fine. However, the next line contains a full record declaration, which doesn't look like a generic formal parameter at all. So the compiler got confused and spit out an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM