我正在尝试为通用链表类重载相等运算符。以下是相关代码:
列表广告:
generic
type Element_Value_Type is private;
package List is
type List_Type is private;
type Element is private;
type Element_Ptr is private;
function "=" (L, R : List_Type) return Boolean;
-- Other linked list function declarations --
private
type Element is
record
Value : Element_Value_Type;
Next : Element_Ptr;
Prev : Element_Ptr;
end record;
type Element_Ptr is access Element;
type List_Type is
record
Length : Integer := 0;
Head : Element_Ptr := null;
Tail : Element_Ptr := null;
end record;
end List;
列表.adb:
package body List is
function "=" (Left, Right : List_Type) return Boolean is
begin
-- Code for equality checking --
end "=";
-- Other function implementations --
end List;
主.adb:
with Text_IO;
with List;
use Ada;
procedure Main is
package Int_Lists is new List (Integer);
procedure Print_List (List : Int_Lists.List_Type) is
begin
-- code to print the contents of a list --
end
L1, L2 : Int_Lists.List_Type;
begin
Int_Lists.Append (L1, 1);
Int_Lists.Append (L2, 1);
Int_Lists.Append (L1, 2);
Int_Lists.Append (L2, 2);
Text_IO.Put_Line (Item => Boolean'Image (L1 = L2));
end Main;
这是我在 Main 正文的最后一行得到的错误:
operator for private type "List_Type" defined at list.ads:X, instance at line X is not directly visible
有没有办法让它看到“=”功能?如果我这样做Int_Lists."=" (L1, L2)
,或者如果我把它放在use Int_Lists
Main 的主体之前,它就可以工作,但是第一种破坏了运算符重载的目的,而第二种允许从 Main 中无限制地访问所有 List 函数。