Edu*_*una 3 operator-overloading ada instantiation
我想实例化一个包并将函数传递给它重载运算符,以便创建一个通用二进制搜索树.这是规格.
bstgen.ads(片段)
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Deallocation;
GENERIC
type Akey is private;
type TreeRecord is private;
with function "<"(K: in Akey; R: in TreeRecord) return Boolean;
with function ">"(K: in Akey; R: in TreeRecord) return Boolean;
with function "="(K: in Akey; R: in TreeRecord) return Boolean;
PACKAGE BSTGen IS
TYPE St10 IS NEW String(1..10);
TYPE TreePt IS PRIVATE;
PACKAGE EnumIO IS NEW Ada.Text_IO.Enumeration_IO(Akey); USE EnumIO;
Run Code Online (Sandbox Code Playgroud)
driver.adb(片段)
with Ada.Text_IO; use Ada.Text_IO;
WITH BSTGen;
PROCEDURE Driver IS
IP: Integer := 1;
TYPE Names IS (Resig,Keene,Marsden,Vuijic,Marcus,Gonzalez);
PACKAGE NamesIO IS NEW Ada.Text_IO.Enumeration_IO(Names);
type St10 is NEW String(1..10);
type Customer is
record Name: Names;
PhoneNumber: St10;
end record;
function "<"(K: in Names; R: in Customer) return Boolean is begin
return K < R.Name;
end "<";
function ">"(K: in Names; R: in Customer) return Boolean is begin
return K > R.Name;
end ">";
function "="(K: in Names; R: in Customer) return Boolean is begin
return K = R.Name;
end "=";
PACKAGE IntIO IS NEW Ada.Text_IO.Integer_IO(Integer); USE IntIO;
PACKAGE mybst is NEW BSTGen(Names,Customer,<,>,=); USE mybst;
R, Pt: TreePt;
Name: Names;
Phone: St10;
Status: Boolean;
BEGIN
R := CreateTree;
Pt := R;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,这是输出:
driver.adb:24:04: expect subprogram or entry name in instantiation of "<"
driver.adb:24:04: instantiation abandoned
bstgen.ads:19:53: expect discrete type in instantiation of "Enum"
bstgen.ads:19:53: instantiation abandoned
Run Code Online (Sandbox Code Playgroud)
这包括一堆错误,指出driver.adb的方法是不可见的,这是因为mybst的实例化被放弃所预期的.我该如何解决?
在bstgen.ads,你说
PACKAGE EnumIO IS NEW Ada.Text_IO.Enumeration_IO(Akey);
Run Code Online (Sandbox Code Playgroud)
但在你说的通用正式部分
type Akey is private;
Run Code Online (Sandbox Code Playgroud)
这意味着编译器可以对实际类型假设很少,除了相等和赋值可用.它可能是数百字节的记录; 它当然不一定是枚举.
要确保这Akey是枚举,您需要说
type Akey is (<>);
Run Code Online (Sandbox Code Playgroud)
在driver.adb,你说
PACKAGE mybst is NEW BSTGen(Names,Customer,<,>,=);
Run Code Online (Sandbox Code Playgroud)
哪个应该读
PACKAGE mybst is NEW BSTGen(Names,Customer,”<“,”>”,”=“);
Run Code Online (Sandbox Code Playgroud)