[英]Ada: how to access this variable?
I have a modified textbook example as follows (the book is Understanding Ada---a software engineering approach by Bray and Pokrass): 我有一个修改过的教科书示例如下(本书是理解Ada --- Bray和Pokrass的软件工程方法):
PACKAGE SOLAR_SYSTEM IS
TYPE PLANET IS (MERCURY, VENUS, MARS, JUPITER, SATURN, NEPTUNE);
SUBTYPE TERRESTRIAL_PLANET IS PLANET RANGE MERCURY..MARS;
SUBTYPE JOVIAN_PLANET IS PLANET RANGE JUPITER..NEPTUNE;
TYPE MILES IS DIGITS 5 RANGE 0.0..5.0E9;
TYPE PLANET_FACTS IS ARRAY (PLANET) OF MILES;
DISTANCE_TO_SUN : CONSTANT PLANET_FACTS :=
(MERCURY => 36.0E6, VENUS => 67.2E6, MARS => 141.7E6,
JUPITER => 484.0E6, SATURN => 887.0E6, NEPTUNE => 2797.0E6);
NUMBER_OF_MOONS: CONSTANT ARRAY (PLANET) OF NATURAL :=
(MERCURY => 0, VENUS => 0, MARS => 2,
JUPITER => 12, SATURN => 10, NEPTUNE => 2);
END SOLAR_SYSTEM;
Now to access some variables and their contents, 现在访问一些变量及其内容,
WITH Ada.Float_Text_IO;
WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
WITH SOLAR_SYSTEM;
USE SOLAR_SYSTEM;
PROCEDURE TEST2 IS
BEGIN -- main program
Ada.Integer_Text_IO.Put(Item => NUMBER_OF_MOONS(Saturn));
Ada.Text_IO.New_Line;
Ada.Float_Text_IO.Put(Item => DISTANCE_TO_SUN(Saturn));
END TEST2;
I can access NUMBER_OF_MOONS(Saturn) without any problem but I have difficulty to access DISTANCE_TO_SUN(Saturn). 我可以毫无问题地访问NUMBER_OF_MOONS(土星),但我很难访问DISTANCE_TO_SUN(土星)。 How to access it?
如何访问它?
thanks a lot... 非常感谢...
This is what I get on compilation with the statement 这是我在使用该语句编译时得到的
Ada.Float_Text_IO.Put(Item => DISTANCE_TO_SUN(Saturn));
list file: 列表文件:
1. WITH Ada.Float_Text_IO;
|
>>> warning: no entities of "FLOAT_TEXT_IO" are referenced
2. WITH Ada.Text_IO;
3. WITH Ada.Integer_Text_IO;
4. WITH SOLAR_SYSTEM;
5. USE SOLAR_SYSTEM;
6.
7.
8. PROCEDURE TEST2 IS
9.
10. BEGIN -- main program
11. Ada.Integer_Text_IO.Put(Item => NUMBER_OF_MOONS(Saturn));
12. Ada.Text_IO.New_Line;
13. Ada.Float_Text_IO.Put(Item => DISTANCE_TO_SUN(Saturn));
1 5
>>> no candidate interpretations match the actuals:
>>> missing argument for parameter "To" in call to "PUT" declared at a-tiflio.ads:81, instance at a-flteio.ads:20
>>> missing argument for parameter "File" in call to "PUT" declared at a-tiflio.ads:63, instance at a-flteio.ads:20
>>> possible missing instantiation of Text_IO.Float_IO
>>> expected type "Standard.Float"
>>> found type "MILES" defined at solar_system.ads:8
>>> ==> in call to "Put" at a-tiflio.ads:70, instance at a-flteio.ads:20
14. END TEST2;
The problem is that DISTANCE_TO_SUN(Saturn) is of type MILES which is one of the floating point types. 问题是DISTANCE_TO_SUN(Saturn)属于MILES类型,它是浮点类型之一。 So just using Ada.Float_Text_IO.Put won't work.
所以只使用Ada.Float_Text_IO.Put将无法正常工作。
The problem is that you've declared a brand new type, MILES, which is "float-like", but is not the predefined float type. 问题是你已经宣布了一个全新的类型MILES,它是“浮动的”,但不是预定义的浮点类型。 So when you try to Put an item of that type, ie DISTANCE_TO_SUN(Saturn), there's no Put procedure available for that type.
因此,当您尝试放置该类型的项目,即DISTANCE_TO_SUN(土星)时,该类型没有可用的Put程序。
You have three primary choices: 您有三个主要选择:
1) Instantiate Float_Text_IO with the Miles type: 1)使用Miles类型实例化Float_Text_IO:
package Miles_IO is new Ada.Text_IO.Float_IO(Solar_System.Miles);
and then use Miles_IO to output the value: 然后使用Miles_IO输出值:
Miles_IO.Put(DISTANCE_TO_SUN(Saturn));
2) Change the definition of miles to be a constrained subtype of Float: 2)将里程的定义更改为Float的约束子类型:
SUBTYPE MILES IS FlOAT RANGE 0.0..5.0E9;
Then the original Float_Text_IO.Put will work just fine. 那么原来的Float_Text_IO.Put就可以了。
3) Since Miles is a floating point type, you can convert it before Putting it: 3)由于Miles是浮点类型,你可以在放置它之前转换它:
Ada.Float_Text_IO.Put(Item => Float(DISTANCE_TO_SUN(Saturn)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.