gpt4 book ai didi

ada - 编译器不允许我获取字符串

转载 作者:行者123 更新时间:2023-12-04 00:54:08 24 4
gpt4 key购买 nike

一位用户最近发布了一个问题,并将其删除(可能是因为我们不太欢迎)。实际上,这就是问题所在:用 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 , 它隐藏了标准类型 String . Ada.Text_IO.Get需要标准参数 String类型,但它实际上被赋予了本地 String 的参数类型。

Ada Wikibook说,在名称等价的要点下,

Two types are compatible if and only if they have the same name; not if they just happen to have the same size or bit representation. You can thus declare two integer types with the same ranges that are totally incompatible, or two record types with exactly the same components, but which are incompatible.

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

毕竟,他们不这样做的原因是完全限定名称实际上是不同的。 Get期待 Standard.String ( ARM 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;

关于ada - 编译器不允许我获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64032013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com