Ada编程记录数组

时间:2014-10-02 13:10:12

标签: ada

我是Ada的新手,我正在尝试创建一个记录数组,然后将一些记录放到数组中,但我收到错误nested array aggregate expected。这是我的代码:

with Ada.Text_IO; use Ada.Text_IO;   
with Ada.Integer_Text_Io;
with Ada.unchecked_conversion;

procedure main is
  type Byte is range 0..255;
  for Byte'Size use 8;

  type Pixel is record
    R:Byte;
    G:Byte;
    B:Byte;
  end record;
  for Pixel'Size use 24;

  r1:Pixel := (1,2,5);
  r2:Pixel := (1,2,3);
  r3:Pixel := (1,2,3);

  type Image is array(Positive range <>, Positive range <>) of Pixel;
  Pragma Pack(Image);

  Left:Image(1..3, 1..1) := (r1, r2, r3);
begin
    null;
end main;

1 个答案:

答案 0 :(得分:5)

二维数组需要二维聚合,即每个元素也是聚合的聚合。例如:

type Integer_Matrix is array (Positive range <>, Positive range <>) of Integer;
M : Integer_Matrix (1..2, 1..2) := ( (1, 2), (3, 4) );

当任一维度的长度为1时,它需要特殊处理,因为括号中的单个值不被视为聚合。需要将单元素聚合写为(1 =&gt; Value)[必要时使用实际索引代替1]。在你的情况下,Image的每一行都有长度1.所以你聚合将有三个元素,每个元素将是另一个长度为1的聚合。它需要看起来像这样:

Left : Image (1..3, 1..1) := ( (1=>r1), (1=>r2), (1=>r3) );