【问题标题】:GNAT Studio ADA getting error "expected type "Standard.Integer"GNAT Studio ADA 出现错误 \"expected type \"Standard.Integer\"
【发布时间】:2022-02-03 13:49:33
【问题描述】:

在这个Intro to Ada Course section about Arrays 中,它表明我可以使用用户定义的类型“Index”来索引一个数组,但是当我尝试使用用户定义的类型来索引一个数组时,它会显示预期的类型“Standard.Integer”。我问这个的原因是因为它明确指出您可以使用任何离散类型来索引数组。

procedure Cipher is
   type Byte is mod 2**8;
   type BufferArray is array ( 0 .. 15 ) of Byte;
   type Index is range 1 .. 16;
   Buffer: BufferArray := (0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
   buber: Byte := 255;
begin
   --  Insert code here.
   for I in Index loop
      Put( Byte'Image(Buffer(I)) ); --error shows up here
   end loop;
   null;
end Cipher;

这个特定版本的 GNAT 是否有问题?

【问题讨论】:

    标签: arrays types ada gnat


    【解决方案1】:

    您将数组定义为使用您自己定义的类型进行索引:

    type BufferArray is array (Index) of Byte;
    

    作为奖励,此定义会自动删除代码中的一个错误。

    【讨论】:

    • 索引类型 1 .. 16 和数组索引类型 0 .. 15 是不同的类型。顺便说一句,这是艾达的强项之一。
    猜你喜欢
    • 1970-01-01
    • 2021-01-08
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多