是否可以在Ada中实例化具有标记记录类型的Map容器​​以使用多态?

时间:2012-12-12 21:44:00

标签: map polymorphism containers ada

我想要一个包含标记记录类型的地图容器,所以我编写了这个程序,但是GNAT编译器没有编译它:

type http_response is tagged private;
package map_package is new Ada.Containers.Ordered_Maps
    (Key_Type => Unbounded_String,
     Element_Type => http_response);

我还有那些编译错误:

http.ads:47:04: instantiation error at a-coorma.ads:199
http.ads:47:04: premature use of type with private component
http.ads:47:105: premature use of private type

确实我想使用多态,因为我的地图将包含其他从http_response类型继承的标记记录类型。

如何更正此代码?

如果我更正这样的代码:

package map_package is new Ada.Containers.Ordered_Maps
    (Key_Type => Unbounded_String,
     Element_Type => http_response'Class);

我得到了这种错误:

http.ads:47:04: instantiation error at a-coorma.ads:195
http.ads:47:04: class-wide subtype with unknown discriminants in component declaration
http.ads:47:04: instantiation error at a-coorma.ads:199
http.ads:47:04: premature use of type with private component
http.ads:47:118: premature use of type with private component

1 个答案:

答案 0 :(得分:3)

3.3 Objects and Named Numbers中所述,“一个全班的子类型被定义为具有未知的判别式,因此是一个不确定的子类型。”您可以实例化Ada.Containers.Ordered_Maps。而不是Ada.Containers.Indefinite_Ordered_Maps