首页 文章

Ada编程记录数组

提问于
浏览
0

我是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 回答

  • 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 => Value)[必要时使用实际索引代替 1 ] . 在你的情况下,Image的每一行都有长度1.所以你聚合将有三个元素,每个元素将是另一个长度为1的聚合 . 它将需要如下所示:

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

相关问题