我正在通过尝试https://adventofcode.com/2018/问题来学习Ada。
我有ActivityVector
个记录的向量ActivityRecord
:
type ActivityRecord is
record
dt: Ada.Calendar.Time;
str: UStr.Unbounded_String;
end record;
package ActivityVector is new Ada.Containers.Vectors
(Element_Type => ActivityRecord,
Index_Type => Natural);
我想将它们放在键为Integer
的地图中。我有以下内容:
function IntegerHash(i: Integer) return Ada.Containers.Hash_Type;
package ActivityMap is new Ada.Containers.Indefinite_Hashed_Maps(
Key_Type => Integer,
Element_Type => Activity.ActivityVector.Vector,
Hash => IntegerHash,
Equivalent_Keys => "="
);
当我尝试编译它时,我得到:
act_map.ads:9:04: instantiation error at a-cihama.ads:46
act_map.ads:9:04: no visible subprogram matches the specification for "="
act_map.ads:9:04: instantiation error at a-cihama.ads:46
act_map.ads:9:04: default "=" on "Vector" is not directly visible
似乎正在期待为向量定义的相等运算符? 我可以定义一个,但首先我要检查一下:
答案 0 :(得分:5)
似乎期望为向量定义一个相等运算符
是的
我可以定义一个
请勿这样做,只需使用Ada.Containers.Vectors
实例化中定义的现有功能即可:
package ActivityMap is new Ada.Containers.Indefinite_Hashed_Maps(
Key_Type => Integer,
Element_Type => Activity.ActivityVector.Vector,
Hash => IntegerHash,
Equivalent_Keys => "=",
"=" => Activity.ActivityVector."="
);
或者,通过执行操作使Activity.ActivityVector."="
函数直接可见
use type Activity.ActivityVector.Vector;