编译器不会让我获取字符串

问题描述

一个用户最近发布了一个问题,并将其删除(可能是因为我们不那么欢迎)。实际上,这就是问题所在:使用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;

这很令人困惑。发生了什么事?

解决方法

麻烦的是,这段代码定义了自己的类型String,它隐藏了标准类型StringAda.Text_IO.Get期望使用标准String类型的参数,但实际上已获得了本地String类型的参数。

Ada Wikibook在项目符号要点名称等效

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

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

之所以没有,是因为完全限定名称实际上是不同的。 Get预期为Standard.StringARM A.1(37)),但本地版本为Person_1.String

您可能希望-gnatwh(打开隐藏声明警告)会报告此消息,但不幸的是,不会。

我不确定为什么编译器只报告Ada.Integer_Text_IO(带有Integer)内的失败匹配;为什么?如果我们删除这withuse(毕竟没用),事情会变得很有帮助,

     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;