【发布时间】:2018-06-02 15:34:12
【问题描述】:
我想用 Ada 制作添加数组元素的子程序。 子程序“Add_Data”有 3 个参数—— 第一个参数 = 泛型数组(INTEGER 数组或 REAL 数组) 第二个参数 = INTEGER(数组大小) 第三个参数 = 泛型 sum(INTEGER 数组 -> sum 为 INTEGER,REAL 数组 -> sum 为 REAL)
我从 ideone.com 对其进行了编程。 (我只想通过 INTEGER 数组查看结果。之后,我将通过 REAL 数组进行测试)
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
procedure test is
generic
type T is private;
type Tarr is array (INTEGER range <>) of T;
--function "+" (A,B : T) return T;
--function "+" (A, B : T) return T is
--begin
-- return (A+B);
--end "+";
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T);
procedure Add_Data(X : in Tarr; Y : in INTEGER; Z : in out T) is
temp : T;
count : INTEGER;
begin
count := 1;
loop
temp :=temp+ X(count); //<-This is problem.
count := count + 1;
if count > Y then
exit;
end if;
end loop;
Z:=temp;
end Add_Data;
type intArray is array (INTEGER range <>) of INTEGER;
intArr : intArray := (1=>2, 2=>10, 3=>20, 4=>30, 5=>8);
sum : INTEGER;
procedure intAdd is new Add_Data(Tarr=>intArray, T=>INTEGER);
begin
sum := 0;
intAdd(intArr, 5, sum);
put (sum);
end test;
当我不重载运算符“+”时,它会出错。 “没有为私有类型“T”定义的适用运算符“+”。 我可以为此做些什么?
【问题讨论】: