John Barnes撰写的“ Ada 2012中的编程”的第53页共享了我无法使用的不完整代码片段。
我想出了一个完整的程序来扩展本书中的代码...
with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Discrete_Random(Coin);
use Random_Coin;
G : Generator;
C : Coin;
begin
for i in 1 .. 20 loop
C := Random(G);
Put (C'Image);
end loop;
end Main;
Run Code Online (Sandbox Code Playgroud)
我正在使用的“ GPS” IDE抱怨以下错误:
IDE确实给了我“ intellisense”(使用Visual Studio中的术语),它指示Discrete_Random实际上是可见的,并且在添加了“ with”和“ use”语句的情况下可用。
有人可以引导我解决我犯下的愚蠢错误吗?
问题是,不像Ada.Numerics.Pi,其中Pi是的一个组成部分Ada.Numerics,Discrete_Random是一个孩子Ada.Numerics。
说完之后use Ada.Numerics,您可以只Pi在程序中编写代码,但实际上必须with Ada.Numerics.Discrete_Random使它可用。
实际上,您不需要with或use Ada.Numerics,这很好用:
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
type Coin is (Heads, Tails);
package Random_Coin is new Ada.Numerics.Discrete_Random(Coin);
Run Code Online (Sandbox Code Playgroud)