没有候选解释与 ADA 中的泛型编码问题匹配
No candidate interpretations match the actuals, problem coding with generics in ADA
由于带有 put 的打印语句,代码无法编译时遇到一些问题。请问下面的代码post。
-- createlis.ads
generic
size: integer;
type itemtype is private;
package createlis is
procedure addtolist( ad : in itemtype );
procedure printlist;
procedure printlist1(pt: in integer );
procedure deletelist(pt: in integer );
procedure listlength;
end createlis;
-- createlis.adb
with ada.text_io; use ada.text_io;
package body createlis is
len: integer;
list: array(1..size) of itemtype;
procedure addtolist( ad : in itemtype ) is
begin
if len < size then
len := len +1;
list(len) := ad;
end if;
end addtolist;
procedure printlist is
begin
for i in 1..len loop
put((list(i)));
put(" , ");
end loop;
end printlist;
procedure printlist1( pt: in integer ) is
begin
put((list(pt)));
end printlist1;
procedure deletelist( pt: in integer) is
begin
for k in pt..(len-1) loop
list(k) := list(k+1);
end loop;
len := len - 1;
end deletelist;
procedure listlength is
i : integer;
begin
i := 0;
for x in 1..size loop
i := i + 1;
end loop;
put("the array is ");
put(integer'image(i));
put(" numbers.");
end listlength;
end createlis;
-- main.adb
with Ada.Text_IO; use Ada.Text_IO;
with createlis;
procedure main is
package iio is new ada.text_io.integer_io(integer); use iio;
package iii is new ada.text_io.float_io(float); use iii;
size: integer;
begin
put("enter size for option c: ");
get(size);
declare
package clist is new createlis(size, integer); use clist;
begin
clist.printlist;
clist.addtolist(12);
clist.addtolist(3);
clist.addtolist(7);
clist.printlist;
new_line;
clist.printlist1(2);
new_line;
clist.listlength;
new_line;
clist.deletelist(2);
clist.printlist;
end;
declare
package blist is new createlis(size, float); use blist;
begin
blist.printlist;
blist.addtolist(12.7);
blist.addtolist(3.5);
blist.addtolist(7.6);
blist.printlist;
new_line;
blist.listlength;
new_line;
blist.deletelist(2);
blist.printlist;
end;
end main;
Ada.Text_IO 不包含 Integer
和 Float
的 Put
函数。虽然它包含通用包 Integer_IO
和 Float_IO
,但您不能直接使用它们,因为您的通用包可以采用任何一种类型。
这意味着您必须在实例化时告诉您的通用包如何放置 itemtype
,例如:
generic
size: integer;
type itemtype is private;
with procedure Put (Value : itemtype);
package createlis is
-- ...
然后在你的 main 里面,你定义你的推杆:
procedure Put_Integer (Value : Integer) is
begin
iio.Put (Value);
end Put_Integer;
procedure Put_Float (Value : Float) is
begin
iii.Put (Value);
end Put_Float;
并在通用包的实例化中使用它们:
package clist is new createlis
(size, integer, Put_Integer); use clist;
-- ...
package blist is new createlis
(size, float, Put_Float); use blist;
您不能直接将 iio
/ iii
中的过程作为通用参数,因为它们采用具有默认值的附加参数。
由于带有 put 的打印语句,代码无法编译时遇到一些问题。请问下面的代码post。
-- createlis.ads
generic
size: integer;
type itemtype is private;
package createlis is
procedure addtolist( ad : in itemtype );
procedure printlist;
procedure printlist1(pt: in integer );
procedure deletelist(pt: in integer );
procedure listlength;
end createlis;
-- createlis.adb
with ada.text_io; use ada.text_io;
package body createlis is
len: integer;
list: array(1..size) of itemtype;
procedure addtolist( ad : in itemtype ) is
begin
if len < size then
len := len +1;
list(len) := ad;
end if;
end addtolist;
procedure printlist is
begin
for i in 1..len loop
put((list(i)));
put(" , ");
end loop;
end printlist;
procedure printlist1( pt: in integer ) is
begin
put((list(pt)));
end printlist1;
procedure deletelist( pt: in integer) is
begin
for k in pt..(len-1) loop
list(k) := list(k+1);
end loop;
len := len - 1;
end deletelist;
procedure listlength is
i : integer;
begin
i := 0;
for x in 1..size loop
i := i + 1;
end loop;
put("the array is ");
put(integer'image(i));
put(" numbers.");
end listlength;
end createlis;
-- main.adb
with Ada.Text_IO; use Ada.Text_IO;
with createlis;
procedure main is
package iio is new ada.text_io.integer_io(integer); use iio;
package iii is new ada.text_io.float_io(float); use iii;
size: integer;
begin
put("enter size for option c: ");
get(size);
declare
package clist is new createlis(size, integer); use clist;
begin
clist.printlist;
clist.addtolist(12);
clist.addtolist(3);
clist.addtolist(7);
clist.printlist;
new_line;
clist.printlist1(2);
new_line;
clist.listlength;
new_line;
clist.deletelist(2);
clist.printlist;
end;
declare
package blist is new createlis(size, float); use blist;
begin
blist.printlist;
blist.addtolist(12.7);
blist.addtolist(3.5);
blist.addtolist(7.6);
blist.printlist;
new_line;
blist.listlength;
new_line;
blist.deletelist(2);
blist.printlist;
end;
end main;
Ada.Text_IO 不包含 Integer
和 Float
的 Put
函数。虽然它包含通用包 Integer_IO
和 Float_IO
,但您不能直接使用它们,因为您的通用包可以采用任何一种类型。
这意味着您必须在实例化时告诉您的通用包如何放置 itemtype
,例如:
generic
size: integer;
type itemtype is private;
with procedure Put (Value : itemtype);
package createlis is
-- ...
然后在你的 main 里面,你定义你的推杆:
procedure Put_Integer (Value : Integer) is
begin
iio.Put (Value);
end Put_Integer;
procedure Put_Float (Value : Float) is
begin
iii.Put (Value);
end Put_Float;
并在通用包的实例化中使用它们:
package clist is new createlis
(size, integer, Put_Integer); use clist;
-- ...
package blist is new createlis
(size, float, Put_Float); use blist;
您不能直接将 iio
/ iii
中的过程作为通用参数,因为它们采用具有默认值的附加参数。