我是Ada代码的初学者。我使用AdaCore的GPS。
我将创建一个由用户确定大小的变量。我写这个:
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure main is
wanted : Integer := 10;
type custom is range 0..wanted;
...
但是第8行出了点问题:
Builder results
C:\Users\**********\Desktop\ada project\src\main.adb
8:26 "wanted" is not static constant or named number (RM 4.9(5))
8:26 non-static expression used for integer type bound
我真的不明白这是什么意思...有人可以帮助我吗?
Variable wanted
不是常数,它可能在程序执行期间改变其值,因此在声明新类型时,不允许将此变量用作范围约束。但是,您可以使用constant
关键字(Wanted : constant Integer := 10;
)使其恒定。它应该可以解决您的问题。
正如帖木儿所说,需要在其范围上必须是恒定的。这使您可以进行一些不错的操作,例如在过程中声明类型。看看这个,可能会很有趣:)
-- My ada program --
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure Main is
procedure Test (Wanted : Integer) is
type custom is new Integer range 0..wanted;
begin
Put_Line("First value " & Custom'Image (Custom'First)
& " Last value " & Custom'Image (Custom'Last));
end Test;
begin
Test (10);
Test (12);
end Main;
输出是
First value 0 Last value 10
First value 0 Last value 12
在这种情况下,您的类型不同于一个调用到另一个调用的类型,但是它在过程中由于wand是常量而起作用。唯一的事情是,定义的类型必须是参数类型的新派生类型。
我让您考虑可能性:)