Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ada ambiguous expression with Get

I am trying to use IOredirection to create, add to, delete from and print two arrays, one of integers and one of floats using generics.

Things are fine for the integer array, but when at "get(fVal)" I am getting the error "ambiguous expression (cannot resolve "Get").

I thought it might have something to do with the float array being after the integer array. I put it before the integer array but still got the same error for the float array.

I am still new to Ada so I am struggling to grasp it sometimes.

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Float_Text_IO;
with CreateList;

procedure Main is

   package IntIO is new Ada.Text_IO.Integer_IO(Integer); use IntIO;
   package FloatIO is new Ada.Text_IO.Float_IO(Float); use FloatIO;

begin

   declare
      max: integer;   --declaring integer named "max"
      val: integer;
      del: integer;
      pointer : integer := 0;
      len : integer := 0;
      procedure MyPut(x: integer) is begin
            IntIO.Put(x, 0, 10);
      end MyPut;

      package C_List is new CreateList(max, integer, MyPut); use C_List;

   begin
      get(max);                              --assigns input for array size and assigns value to "max"
      for row in 1 .. max loop
         get(val);
         C_List.add_to_list(val);            --calls procedure to add value 12 to array
      end loop;
      C_List.print_list;                     --calls procedure to print entire array
      len := C_List.length_of_list;          --calls function to determine the current length of array and assign the value to "len"
      for pointer in 1 .. len loop           --calls procedure to print individual value in array. Loops to print all values in array
         C_List.print_list(pointer);
      end loop;
      Put_Line("");
      get(del);
      C_List.delete_from_list(del);          --calls procedure to delete a value from array
      C_List.print_list;                     --calls procedure to print entire array
   end;

   

   declare
      max: integer;   --declaring integer named "max"
      fVal: float;
      del: integer;
      pointer : integer := 0;
      len : integer := 0;
      
      procedure MyPut(x: Float) is begin
         FloatIO.Put(x, 0, 0, 0);
      end MyPut;


      package B_List is new CreateList(max, float, MyPut); use B_List;

      begin
         get(max);                           --assigns input for array size and assigns value to "max"
      for row in 1 .. max loop
         get(fVal);
         B_List.add_to_list(fVal);           --calls procedure to add value 12 to array
      end loop;
         B_List.print_list;                  --calls procedure to print entire array
         len := B_List.length_of_list;       --calls function to determine the current length of array and assign the value to "len"
         for pointer in 1 .. len loop        --calls procedure to print individual value in array. Loops to print all values in array
            B_List.print_list(pointer);
         end loop;
         Put_Line("");
         get(del);
         B_List.delete_from_list(del);       --calls procedure to delete a value from array
         B_List.print_list;                  --calls procedure to print entire array
      end;

end Main;

createlist.adb

with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Float_Text_IO;

package body CreateList is
   len: Integer;                       --declaring integer named "len"
   list: array(1..max) of itemType;    --making an integer array named "list"
   pnt: integer;                       --declaring integer named "pnt"

   procedure add_to_list (ListItem: in itemType) is begin   --procedure that adds new integer value to array "list"
      if pnt >= max then                                    --detects when array is full and can't fit more values
         Put_Line("**Detected Overflow**");
      else
         pnt := pnt + 1;                                    --moves pointer
         list(pnt) := ListItem;                             --adds new integer value to pointer index
      end if;
   end add_to_list;

   procedure delete_from_list (indx: in Integer) is begin   --procedure that removes an integer value from array "list"
      if indx = 0 then                                      --detects when array is empty and doesn't have any values to delete
         Put_Line("**Detected Underflow**");
      else
         for j in indx .. (pnt - 1) loop                    --loop to pop the stack after an integer has been removed from array
            if j < len then
               list(j) := list(j + 1);
            end if;
         end loop;

         pnt := pnt - 1;
      end if;
   end delete_from_list;

   function length_of_list return Integer is begin   --function to determine the current number of integers in array
      for i in 1 .. pnt loop
         len := len + 1;
      end loop;
      return len;
   end length_of_list;

   procedure print_list (indx: in Integer) is begin   --procedure that prints the integer at a given index of the array
      Put(list(indx));
      Put(" ");
   end print_list;

   procedure print_list is begin   --procedure that prints the entire array
      for i in 1 .. pnt loop
         Put(list(i));
         Put(" ");
      end loop;
      Put_Line("");
   end print_list;

begin
   pnt := 0;
   len := 0;

end CreateList;

createlist.ads

generic
   max: integer;
   type itemType is private;
   
   with procedure Put(x: itemType);

package CreateList is
      
   procedure add_to_list (listItem: in itemType);
   --   
   procedure delete_from_list (indx: in Integer);
   --   
   function length_of_list return Integer;
   --
   procedure print_list (indx: in Integer);
   procedure print_list;

end CreateList;

Thank you @thindil. I changed get(fVal) to FloatIO.get(fVal) and it worked.

like image 907
Jesse Schultz Avatar asked Jun 09 '26 04:06

Jesse Schultz


1 Answers

This error means that the compiler found a few references to the selected subprogram (in your case get) and can't decide which one should use because they all are look valid. In that situation you have to exactly tell the compiler which one you want to use by adding the proper package name. In your case it will be:

get(max); should be replaced by IntIO.get(max);

get(val); should be replaced by IntIO.get(val);

get(fVal); should be replaced by FloatIO.get(fVal);

That's probably all :)

like image 59
thindil Avatar answered Jun 10 '26 20:06

thindil