Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Ada中复制相同类型和大小但不同索引类型的数组_Arrays_Ada_Assign - Fatal编程技术网

Arrays 在Ada中复制相同类型和大小但不同索引类型的数组

Arrays 在Ada中复制相同类型和大小但不同索引类型的数组,arrays,ada,assign,Arrays,Ada,Assign,我正在学习Ada,目前的课程是关于数组的。考虑下面的程序; procedure Main is type T_Bool is array (Boolean) of Integer; type T_Intg is array (Integer range 1 .. 2) of Integer; A1 : T_Bool := (others => 0); A2 : T_Intg := (others => 0); begin -- None of these

我正在学习Ada,目前的课程是关于数组的。考虑下面的程序;
procedure Main is
   type T_Bool is array (Boolean) of Integer;
   type T_Intg is array (Integer range 1 .. 2) of Integer;
   A1 : T_Bool := (others => 0);
   A2 : T_Intg := (others => 0);
begin
   -- None of these statements work
   A2 := A1;
   A2 (1 .. 2) := A1 (false .. true);
   A2 := A1 (Boolean'First .. Boolean'Last);
end Main;
根据Adacore大学导师的说法,只要长度相同,就可以将值从一个数组复制到另一个数组。在代码片段中,尽管长度相同,为什么不能为数组分配相同大小但不同的索引方法


正确的复制方法是什么?这是一种循环遍历布尔类型范围内的所有索引并复制相应数组索引的情况,还是有另一种聪明的方法来执行此操作?

如果数组的类型和长度相同,但起始索引不同,则可以复制数组而不进行任何显式转换:

procedure Test is
    type T is array (Positive range <>) of Integer;    
    A : T(1 .. 10);
    B : T(21 .. 30);
begin
    A := B;
end Test;
因此,是的,在您的示例中,看起来您需要复制循环中的元素。

幻灯片3中说,所有数组都是双类型的,这意味着,与其他类型一样,您不能在两种数组类型之间赋值;所以是的,你需要一个循环


有一些聪明的方法可以使用,例如,未经检查的转换;但是,真的,不要去那里

谢谢你们的回答,伙计们,这绝对是有用的信息。这比我想象的还要深刻。在本教程的测验部分,我了解到即使这也是一个错误

procedure Main is
   type T1 is array (Integer range 1 .. 10) of Integer;
   type T2 is array (Integer range 1 .. 10) of Integer;
   A1 : T1;
   A2 : T2;
begin
   A1 := (others => 0);
   -- Cannot do this
   A2 := A1;
end Main;
A2型和A1型可以用同样的方式定义,但Ada认为它们不相关,因此不兼容。在C++中做

是等价的。
typedef int MyType1;
typedef int MyType2;
MyType1 MyVar1;
MyType2 MyVar2;
// Error - Cannot assign differing types,
// despite them being semantically the same
MyVar2 = MyVar1;

但是T1和T2不是完全相同的类型。一个是T1,另一个是T2。在Ada中,类型是通过名称而不仅仅是数据模型来区分的。@Jacobsparrenadersen-我的意思是,类型被定义为语义相同。我将修改我的回答。
typedef int MyType1;
typedef int MyType2;
MyType1 MyVar1;
MyType2 MyVar2;
// Error - Cannot assign differing types,
// despite them being semantically the same
MyVar2 = MyVar1;