我很确定这是新Ada人的陷阱 . 我觉得有必要做一些简单的事情来做到这一点 . 我有一些旧的代码,带有 Send_Command 过程的声明,带有基于Ada.Strings.Bounded通用模块的 Command_String_Type 类型参数 .
old
-- -- -- command.ads -- -- --
-- nothing relevant, Send_command is/was internal to module.
:
-- -- -- command.adb -- -- --
-- Type to hold command to send while it is being constructed.
package Command_String_Type is
new Ada.Strings.Bounded.Generic_Bounded_Length(Max => Command_Max);
:
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String );
这: Command_String_Type 用作此模块必须从此模块导出的Send_Command过程的参数类型 .
当我尝试在模块规范(. ads )文件中声明Command_String_Type时,我遇到了麻烦 . 我可以't get the syntax straight to '导出'此程序的此类型规范 .
target
-- -- -- command_interface.ads -- -- --
package Command_Interface is
Command_Max : constant := 200;
:
-- WANTED ... export Command_String_Type from this module
-- Type to hold command to send while it is being constructed.
package Command_String_Type is
new Ada.Strings.Bounded.Generic_Bounded_Length(Max => Command_Max);
:
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String );
:
end Command_Interface; -- specification
-- -- -- command_interface.adb -- -- --
package body Command_Interface is
:
procedure Send_Command(
Command_String : in Command_String_Type.Bounded_String )
is
begin
:
-- implementation ...
:
end Send_Command;
end Command_Interface; --package
当然,Ada-95编译器需要一个“Command_String_Type”,如上所示 . 希望将Send_Command过程放在command_interface包中 . 并导出其他模块使用 . 此过程取决于Command_String_Type
-
起初我做了一个直接粘贴的包Command_String_Type是新的Ada.Strings ...构造 - 并收到错误 .
-
这是“目标” Headers 下面的代码 .
-
我认为.ADS文件中的类型声明会起作用,因为错误抱怨"no type" - 我又有错误 .
-
然后我使用Subtype也出错了 .
-
(更新...)声明Command_String_Type为私有 - 仍然存在编译错误
-
我最后的努力是创建一个Command_String_TypeX的Command_String_Type子类型,其思想是我可以在声明中公开子类型 .
到目前为止,我已经阅读了书籍并查看了培训笔记,我发现了一些例子'exports'来自有界字符串的字符串类型 - 我开始怀疑我认为是否像其他人一样直截了当语言 . 这是可能的,并允许与Ada . 提前感谢您的建议 .
1 回答
你提出的规范看起来很好:
身体也是如此(当然这是一个演示):
现在我们来使用它 . 您已在
Command_Interface
的可见部分创建了包Command_String_Type
,因此您可以将其引用为Command_Interface.Command_String_Type
:你可能会认为这一切都变得有点冗长 . 您可以缩短实例化的名称,例如
或者你可以在电话附近使用套餐重命名技巧,
或者你可能会过火而且
use
: