1
votes

Very new with Ada, this is my first time coding with it. Very lost. Any tips, and direction would be great.

Ada question:

I am trying to make: the function Top (S : Stack) return Item_Type, which returns the top item on the stack or raises the Underflow exception, to the generic unbounded stack package.

The function I added for this is at the bottom of this code block. Current errors: invalid use of subtype mark in expression or call actual for "From" must be a variable invalid use of subtype mark in expression or call

package body Unbound_Stack is

   type Cell is record
      Item : Item_Type;
      Next : Stack;
   end record;

   procedure Push (Item : in Item_Type; Onto : in out Stack) is
   begin
      Onto := new Cell'(Item => Item, Next => Onto);  -- '
   end Push;


   procedure Pop (Item : out Item_Type; From : in out Stack) is
   begin
      if Is_Empty(From) then
         raise Underflow;
      else
         Item := From.Item;
         From := From.Next;
      end if;
   end Pop;

   function Is_Empty (S : Stack) return Boolean is
   begin
      return S = null;
   end Is_Empty;

   --added this code, and then had errors!

   function Top (S : Stack) return Item_Type is
   begin
      --Raise the underflow
      if Is_Empty(S) then
         raise Underflow;
      else
         --or return top item from the stack, call pop
         Pop (Item_Type, from => S);--I think I should pull from the stack w/pop
      end if;

      return Item_Type;
   end Top;

end Unbound_Stack;
2
Did you check which line numbers the error messages refer to? - Jacob Sparre Andersen
If you compile the file inside an IDE like GNAT Programming Studio (GPS), the editor will help you find the line, where you make a mistake. - Jacob Sparre Andersen
Hello, I am using GPS on Windows. There are line numbers, but it really doesn't make sense to me. Here is a link to the errors/lines. imgur.com/a/PUrDZ - theSpleen

2 Answers

1
votes

You have two error messages referring to this line:

   Pop (Item_Type, from => S);--I think I should pull from the stack w/pop

The first one is pointing at Item_Type and says "invalid use of subtype mark in expression or call".

  • This means that you are using the name of a type in a place where that isn't allowed. Actual parameters to subprograms can never be types. You need to use (depending on the parameter direction) a variable or an expression for the actual parameter.
1
votes

You're passing in a type (Item_Type) into Pop. Instead you need to declare a variable of type Item_Type and use that.

e.g.

function Top (S : Stack) return Item_Type is

   Popped_Item : Item_Type;

begin
   ...

and then the call to Pop becomes:

   Pop (Item => Popped_Item, From => S)