Generics 带Get的Ada模糊表达式

Generics 带Get的Ada模糊表达式,generics,ada,io-redirection,Generics,Ada,Io Redirection,我正在尝试使用IOredirection创建、添加、删除和打印两个数组,一个是整数,另一个是使用泛型的浮点 对于整数数组来说一切正常,但在“get(fVal)”时,我得到的是“error”模糊表达式(无法解析“get”) 我认为这可能与浮点数组在整数数组之后有关。我把它放在整数数组之前,但浮点数组仍然有相同的错误 我对Ada还是个新手,所以有时候我很难掌握它 with Ada.Text_IO; use Ada.Text_IO; with Ada.Float_Text_IO; use Ada.Fl

我正在尝试使用IOredirection创建、添加、删除和打印两个数组,一个是整数,另一个是使用泛型的浮点

对于整数数组来说一切正常,但在“get(fVal)”时,我得到的是“error”模糊表达式(无法解析“get”)

我认为这可能与浮点数组在整数数组之后有关。我把它放在整数数组之前,但浮点数组仍然有相同的错误

我对Ada还是个新手,所以有时候我很难掌握它

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;
谢谢@thindil。
我将get(fVal)更改为FloatIO.get(fVal),它成功了。

您收到此消息的原因是您有两个包提供了一个put for Float:

  • 您的Ada.Text\u IO.Float\u IO(Float)实例名为FloatIO

  • float的标准包名为Ada.float\u Text\u IO

根据您的需要,您可以同时使用和前缀,也可以只使用Ada.Float\u Text\u IO,因为您没有特定的浮点类型

在错误消息中不太容易看到:

main.adb:23:40: warning: "max" may be referenced before it has a value
main.adb:61:10: ambiguous expression (cannot resolve "Get")
main.adb:61:10: possible interpretation at a-tiflio.ads:57, instance at line 9
main.adb:61:10: possible interpretation at a-tiflio.ads:57, instance at a-flteio.ads:20
gnatmake: "main.adb" compilation error
唯一的线索是最后一行,它告诉我们解释来自a-flteio.ads中第20行的实例,这是Ada.Float\u Text\u IO的文件名


需要注意的是,尽管Ada.Text\u Text\u IO是一个类似于您的实例化(即新的Ada.Text\u IO.Float\u IO(Float)),但从编译器的角度来看,它不是同一个包。

此错误意味着编译器找到了对所选子程序的一些引用(在您的例子中,
get
)无法决定应该使用哪一个,因为它们看起来都是有效的。在这种情况下,您必须通过添加正确的包名准确地告诉编译器您要使用哪一个。在您的情况下,它将是:

get(max);
应替换为
IntIO.get(max);

get(val);
应替换为
IntIO.get(val);

get(fVal);
应替换为
FloatIO.get(fVal);


这可能就是全部:)

请注意,源文件名在Ada中并不重要。Ada不关心通用pkg Createlist的规范是否在Createlist.ads或CL.spc中,或者根本不在文件中。我们也不在乎。(请注意,有些编译器更挑剔,因此当/如果您询问特定的编译器时,文件名可能很重要。)
不明确的表达式(无法解析“Get”).
这之后通常会有一个找到的看似合理的解决方案列表。查看此列表通常会明确为什么它无法解决您想要的问题。我相信Frederic已经解决了具体问题,但更一般地说,Gnat编译错误消息往往会提供帮助。解决问题似乎没有问题g整数。有时完全解析的名称是必需的,有时它们为读者增加了清晰度,有时它们只是杂乱无章。@BrianDrummond,对我来说这是个问题,GNAT不想编译示例代码,直到我没有澄清它。因此,它可能还取决于编译器版本。
main.adb:23:40: warning: "max" may be referenced before it has a value
main.adb:61:10: ambiguous expression (cannot resolve "Get")
main.adb:61:10: possible interpretation at a-tiflio.ads:57, instance at line 9
main.adb:61:10: possible interpretation at a-tiflio.ads:57, instance at a-flteio.ads:20
gnatmake: "main.adb" compilation error