operator-overloading - Ada Compiler 放弃通过重载运算符的包的实例化

标签 operator-overloading instantiation ada

我想实例化一个包并向其传递重载运算符的函数,以创建通用二叉搜索树。这是规范。

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;

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;

但是,当我尝试编译时,这是输出:

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

这包括一堆错误,指出 driver.adb 的方法不可见,这是预期的,因为 mybst 的实例化被放弃了。我该如何解决这个问题?

最佳答案

bstgen.ads中,你说

PACKAGE EnumIO IS NEW Ada.Text_IO.Enumeration_IO(Akey);

但是在你所说的通用正式部分

type Akey is private;

这意味着编译器可以对实际类型做出很少的假设,除了相等和赋值是可用的。它可能是数百字节的记录;它当然不必是枚举。

要确保Akey是一个枚举,你需要说

type Akey is (<>);

ARM12.5(13) .

driver.adb中,你说

PACKAGE mybst is NEW BSTGen(Names,Customer,<,>,=);

应该读什么

PACKAGE mybst is NEW BSTGen(Names,Customer,”<“,”>”,”=“);

关于operator-overloading - Ada Compiler 放弃通过重载运算符的包的实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33848077/

相关文章:

c++ - 对类实例和 vector 使用赋值运算符

c++ - 将字符传递给运算符重载的自定义流操纵器

ada - Ada 中的任意长度整数

c++ - 枚举比较运算符的重新定义

java - java和c#之间的静态上下文差异无法引用非静态变量

ios - instantiateViewControllerWithIdentifier 似乎调用了 viewdidload

java - 如何根据用户输入实例化数组

Ada - 打印持续时间数据类型时如何控制小数位数?

erlang - 用 Ada 写的 Erlang?

C++:返回重载 [] 和 "this"