简体   繁体   English

以Ada标记类型创建2D数组字段

[英]Creating a 2D-Array Field in an Ada Tagged Type

I'm trying to create a Java/C class equivalent in Ada. 我正在尝试在Ada中创建等效的Java / C类。 From what I've researched -- the equivalent would be a Tagged Record. 根据我的研究-等同于标记记录。 I'm wanting to create a field that is a 2D Array, however with the code below -- it gives me the following errors. 我想创建一个二维数组的字段,但是使用下面的代码-它给我以下错误。

Code: 码:

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;

Error: 错误:

foo.ads:6:25: missing "end record;" for "record" at line 8

foo.ads:7:17: no "record" for this "end record"

This tells me that "Anonymous arrays are not allowed as components". 这告诉我“不允许将匿名数组作为组件”。

package Foo is
type Bar is tagged
    record
        field1 : Integer;
        field2 : Integer;
        field3 : array (1 .. 10, 1 .. 5) of Integer;
    end record;
end Foo;

The reason your second version fails is that 您的第二个版本失败的原因是

field3 : array (1 .. 10, 1 .. 5) of Integer;

declares a new, unnamed (anonymous), type ( array (1 .. 10, 1 .. 5) of Integer ), and you're not allowed to nest type declarations. 声明一个新的未命名(匿名)类型( array (1 .. 10, 1 .. 5) of Integerarray (1 .. 10, 1 .. 5) of Integer )),并且不允许嵌套类型声明。

Depending on your application, you might try 根据您的应用程序,您可以尝试

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;

or 要么

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;

[Hmm. [嗯。 I said First, Second : Natural to allow for zero-length arrays. 我说的First, Second : Natural允许零长度数组。 But I'm not sure what the point of A_Bar : Bar (0, 10); 但是我不确定A_Bar : Bar (0, 10); would be!] 将会!]

You attempt to declare one named type inside the declaration of another named type. 您试图在另一个命名类型的声明内声明一个命名类型。 You can't do that. 你不能那样做。

Also, Ada does not allow record fields of anonymous types, so you have to ensure that you have declared all the types you need for the fields of your record, before the declaration of the record itself. 另外,Ada不允许匿名类型的记录字段,因此,在声明记录本身之前,必须确保已声明记录字段所需的所有类型。

Are you sure you need values in the range -32_768 .. 32_767 (that's all Ada promises about the type Integer )? 您确定需要-32_768 .. 32_767范围内的值(这是Ada对类型Integer承诺)? Normally one would declare a type matching the requirements of the problem. 通常,将声明一种符合问题要求的类型。 (Of course, if the problem is to match the C type int closely, then one should use Interfaces.C.int .) (当然,如果问题是紧密匹配C类型int ,则应该使用Interfaces.C.int 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM