[英]Creating a 2D-Array Field in an Ada Tagged Type
我正在尝试在Ada中创建等效的Java / C类。 根据我的研究-等同于标记记录。 我想创建一个二维数组的字段,但是使用下面的代码-它给我以下错误。
码:
package Foo is
type Bar is tagged
record
field1 : Integer;
field2 : Integer;
type field3 is array (1 .. 10, 1 .. 5) of Integer;
end record;
end Foo;
错误:
foo.ads:6:25: missing "end record;" for "record" at line 8
foo.ads:7:17: no "record" for this "end record"
这告诉我“不允许将匿名数组作为组件”。
package Foo is
type Bar is tagged
record
field1 : Integer;
field2 : Integer;
field3 : array (1 .. 10, 1 .. 5) of Integer;
end record;
end Foo;
您的第二个版本失败的原因是
field3 : array (1 .. 10, 1 .. 5) of Integer;
声明一个新的未命名(匿名)类型( array (1 .. 10, 1 .. 5) of Integer
( array (1 .. 10, 1 .. 5) of Integer
)),并且不允许嵌套类型声明。
根据您的应用程序,您可以尝试
type Twod_Array is array (Integer range <>, Integer range <>) of Integer;
type Bar is tagged
record
field1 : Integer;
field2 : Integer;
field3 : Twod_Array (1 .. 10, 1 .. 5);
end record;
要么
type Twod_Array is array (Positive range <>, Positive range <>) of Integer;
type Bar (First, Second : Natural) is tagged
record
field1 : Integer;
field2 : Integer;
field3 : Twod_Array (1 .. First, 1 .. Second);
end record;
[嗯。 我说的First, Second : Natural
允许零长度数组。 但是我不确定A_Bar : Bar (0, 10);
将会!]
您试图在另一个命名类型的声明内声明一个命名类型。 你不能那样做。
另外,Ada不允许匿名类型的记录字段,因此,在声明记录本身之前,必须确保已声明记录字段所需的所有类型。
您确定需要-32_768 .. 32_767
范围内的值(这是Ada对类型Integer
承诺)? 通常,将声明一种符合问题要求的类型。 (当然,如果问题是紧密匹配C类型int
,则应该使用Interfaces.C.int
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.