2012-12-12 17 views
2

我想有一个地图容器,其中将包含一个标签的记录类型,所以我写了这个程序,但GNAT编译器不会编译:为了使用多态性,可以在Ada中实例化一个带有标记记录类型的Map容器​​?

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 
+0

http_request或http_response?声明和泛型参数不匹配。但是如果你想要多态性,我认为你需要类的类型:'Element_Type => http_response'class' –

+0

@Brian Drummond:我的地图将conatin http_response,我刚刚做了一个复制粘贴错误。当我添加“'类”,我有这些错误:http.ads:47:04:实例化错误在a-coorma.ads:195 http.ads:47:04:在组件声明中具有未知区分的类的子类型 http.ads:47:04:实例化错误在a-coorma.ads:199 http.ads:47:04:过早使用类型与私人组件 http.ads:47:118:过早使用类型与私人组件 – backlash

+0

请更新您的问题(a)更正复制粘贴错误,并(b)包含错误消息,格式化为代码以便于阅读。在您编辑时,您可能会在标题中将“ADA”更改为“Ada”;这不是一个缩写。欢迎来到Stack Overflow! –

回答

3

正如3.3 Objects and Named Numbers提到,“A类范围的子类型被定义为具有未知的判别,因此是一个不确定的子类型。“而不是Ada.Containers.Ordered_Maps,您可能能够实例化Ada.Containers.Indefinite_Ordered_Maps

+0

参见[* Ada 2005的基本原理:8.5无限容器*](http://www.adaic.org/resources/add_content/standards/05rat/html/Rat-8-5.html)。 – trashgod

+0

感谢您的提议,我有这个错误:http.ads:47:116:私人类型的过早使用 – backlash

+0

感谢您的回答,我找到了解决方案。这只是一个可见性问题。 – backlash

相关问题