hGen
hGen

Reputation: 2285

ada invalid index constraint

I defined a matrix type in ada like this:

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

in order to apply some transformations to an input matrix, I need to define a matrix slice in a function.

I tried that by doing it the following way

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;

However, compiling fails with the error: invalid index constraint which I don't quite understand, since Putting A'Length returns a number as A'Length /2 does. If I declare C using fixed numbers like that

 C: Matrix_Type(2,2);

everything works fine.

What is the error in that case, the only possible case in which I would understand it kind of would be if I pass some uninitialized Matrix to the function, and even that doesn't quite make sense to me.

Upvotes: 0

Views: 578

Answers (1)

trashgod
trashgod

Reputation: 205785

The index constraint for the matrix C should be a range:

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;

For non-square matrices, you can use the Operations of Array Types to specify a particular index:

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

Upvotes: 6

Related Questions