2

我想要一个包含标记记录类型的地图容器,所以我编写了这个程序,但 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
4

1 回答 1

3

正如3.3 对象和命名数字中提到的,“一个类范围的子类型被定义为具有未知的判别式,因此是一个不确定的子类型。” 取而代之的是Ada.Containers.Ordered_Maps,您可以实例化Ada.Containers.Indefinite_Ordered_Maps.

于 2012-12-13T02:14:45.070 回答