ada无效索引约束

时间:2017-03-16 19:24:54

标签: matrix slice ada

我在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;

但是,编译因错误而导致失败:invalid index constraint我不太了解,因为将A'Length返回为A'Length /2的数字。如果我使用固定数字声明C

 C: Matrix_Type(2,2);

一切正常。

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

1 个答案:

答案 0 :(得分:6)

矩阵C的{​​{3}}应为index constraint

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;

对于非方形矩阵,您可以使用range指定特定索引:

C : Matrix_Type(M'First(1) .. M'Length(1) / 2, M'First(2) .. M'Length(2) / 2);