编译器不会让我得到一个字符串

Sim*_*ght 3 ada

一位用户最近发布了一个问题,然后将其删除(可能是因为我们不太欢迎)。实际上,这就是问题所在:用 编译gnatmake -gnatwl person_1.adb,结果是

     1. with Ada.Text_IO;                    use Ada.Text_IO;
     2. with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
     3. procedure Person_1 is
     4.    type String is
     5.      array (Positive range <>) of Character;
     6.    S: String (1..10);
     7. begin
     8.    Put("Write a name: ");
     9.    Get(S);
           1   6
        >>> no candidate interpretations match the actuals:
        >>> missing argument for parameter "Item" in call to "Get" declared at a-tiinio.ads:90, instance at a-inteio.ads:18
        >>> missing argument for parameter "Item" in call to "Get" declared at a-tiinio.ads:51, instance at a-inteio.ads:18
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:451
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:378
        >>> expected type "Standard.Integer"
        >>> found type "String" defined at line 4
        >>>   ==> in call to "Get" at a-tiinio.ads:59, instance at a-inteio.ads:18
        >>>   ==> in call to "Get" at a-textio.ads:454
        >>>   ==> in call to "Get" at a-textio.ads:381

    10. end Person_1;
Run Code Online (Sandbox Code Playgroud)

这很令人困惑。这是怎么回事?

Sim*_*ght 6

麻烦的是,这段代码定义了自己的 type String,它隐藏了标准 type StringAda.Text_IO.Get期望一个标准String类型的参数,但它实际上被赋予了一个本地String类型的参数。

阿达维基教科书说,子弹点下名称相同

两种类型兼容当且仅当它们具有相同的名称;如果它们恰好具有相同的大小或位表示,则不会。因此,您可以声明两个具有相同范围但完全不兼容的整数类型,或者声明两个具有完全相同组件但不兼容的记录类型。

但是,这两种类型确实具有相同的名称!( String). 不是吗?

毕竟,它们没有的原因是完全限定的名称实际上是不同的。Get期望Standard.StringARM A.1(37)),但本地版本是Person_1.String.

您可能希望-gnatwh(打开隐藏声明的警告)会报告这一点,但不幸的是没有。

我不确定为什么编译器只报告Ada.Integer_Text_IO(with Integer)内的失败匹配;如果我们删除它with并且use(毕竟它没有被使用),事情会变得更有帮助,

     1. with Ada.Text_IO;                    use Ada.Text_IO;
     2. --  with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
     3. procedure Person_1 is
     4.    type String is
     5.      array (Positive range <>) of Character;
     6.    S: String (1..10);
     7. begin
     8.    Put("Write a name: ");
     9.    Get(S);
           1   4
        >>> no candidate interpretations match the actuals:
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:451
        >>> missing argument for parameter "Item" in call to "Get" declared at a-textio.ads:378
        >>> expected type "Standard.String"
        >>> found type "String" defined at line 4
        >>>   ==> in call to "Get" at a-textio.ads:454
        >>>   ==> in call to "Get" at a-textio.ads:381

    10. end Person_1;
Run Code Online (Sandbox Code Playgroud)