【发布时间】:2012-10-21 13:26:16
【问题描述】:
我正在尝试为通用链表类重载相等运算符。以下是相关代码:
list.ads:
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;
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;
main.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函数.
【问题讨论】:
标签: generics operator-overloading ada