ada无效索引约束

hGe*_*Gen 0 ada matrix slice

我在ada中定义了一个矩阵类型,如下所示:

type Matrix_Type is array(Natural range <>, Natural range <>) of Item_Type;

为了将一些变换应用于输入矩阵,我需要在函数中定义矩阵切片.

我通过以下方式尝试了这一点

procedure Do_Stuff(M: Matrix_Type) is
   -- c needs to be half as big as the input matrix M
   C: Matrix_Type(A'Length / 2, A'Length / 2);
begin
   ...
end Do_Stuff;
Run Code Online (Sandbox Code Playgroud)

但是,编译因错误invalid index constraint而失败:我不太明白,因为Putting会A'Length返回一个数字A'Length /2.如果我用这样的固定数字声明C.

 C: Matrix_Type(2,2);
Run Code Online (Sandbox Code Playgroud)

一切正常.

在这种情况下,错误是什么,唯一可能的情况是,如果我将一些未初始化的矩阵传递给函数,我会理解它,即使这对我来说也没有意义.

tra*_*god 6

矩阵的 索引约束C应该是一个范围:

procedure Do_Stuff(M: Matrix_Type) is
   -- C needs to be half as big as the input matrix M
   C : Matrix_Type(M'First .. M'Length / 2, M'First .. M'Length / 2);
begin
   …
end Do_Stuff;
Run Code Online (Sandbox Code Playgroud)

对于非方形矩阵,您可以使用" 数组类型操作"来指定特定索引:

C : Matrix_Type(M'First(1) .. M'Length(1) / 2, M'First(2) .. M'Length(2) / 2);
Run Code Online (Sandbox Code Playgroud)