基本上,我有2个文件(.adb和.ads)。我是Ada的新手,也是如何编译2个文件的。该程序是一个基本的堆栈实现。编译.adb文件时出现了编译错误。
$ gcc -c test_adt_stack.adb
abstract_char_stack.ads:22:01: end of file expected, file can have only one compilation unit
我拥有的2个文件是: 的 abstract_char_stack.ads
-----------------------------------------------------------
package Abstract_Char_Stack is
type Stack_Type is private;
procedure Push(Stack : in out Stack_Type;
Item : in Character);
procedure Pop (Stack : in out Stack_Type;
Char : out Character);
private
type Space_Type is array(1..8) of Character;
type Stack_Type is record
Space : Space_Type;
Index : Natural := 0;
end record;
end Abstract_Char_Stack;
-----------------------------------------------------------
package body Abstract_Char_Stack is
----------------------------------------------
procedure Push(Stack : in out Stack_Type;
Item : in Character) is
begin
Stack.Index := Stack.Index + 1;
Stack.Space(Stack.Index) := Item;
end Push;
--------------------------------------------
procedure Pop (Stack : in out Stack_Type;
Char : out Character) is
begin
Char := Stack.Space(Stack.Index);
Stack.Index := Stack.Index - 1;
end Pop;
--------------------------------------------
end Abstract_Char_Stack;
另一个是 test_adt_stack.adb
-----------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Abstract_Char_Stack; use Abstract_Char_Stack;
procedure Test_ADT_Stack is
S1 : Stack_Type;
S2 : Stack_Type;
Ch : Character;
begin
Push(S1,'H'); Push(S1,'E');
Push(S1,'L'); Push(S1,'L');
Push(S1,'O'); -- S1 holds O,L,L,E,H
for I in 1..5 loop
Pop(S1, Ch);
Put(Ch); -- displays OLLEH
Push(S2,Ch);
end loop; -- S2 holds H,E,L,L,O
New_Line;
Put_Line("Order is reversed");
for I in 1..5 loop
Pop(S2, Ch);
Put(Ch); -- displays HELLO
end loop;
end Test_ADT_Stack;
-----------------------------------------------------------
我做错了什么?我只想让它编译并显示它应该做的事情。这是一项研究计划的任务。但我无法编译或不知道我是否做得对。
答案 0 :(得分:8)
问题是GNAT [和FSF GNAT是GCC使用的,IIRC]不允许在单个文件中使用多个编译单元。 (这是由于他们如何管理图书馆,但对于初学者来说,这可能有点太详细了。)
解决方案,每个都需要自己的文件:
abstract_char_stack.ads
)abstract_char_stack.adb
)test_adt_stack.adb
)答案 1 :(得分:0)
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
-----------------------------------------------------------
procedure Test_ADT_Stack is
enter code here
package Abstract_Char_Stack is
type Stack_Type is private;
procedure Push(Stack : in out Stack_Type;
Item : in Character);
procedure Pop (Stack : in out Stack_Type;
Char : out Character);
private
type Space_Type is array(1..8) of Character;
type Stack_Type is record
Space : Space_Type;
Index : Natural := 0;
end record;
end Abstract_Char_Stack;
use Test_ADT_Stack.Abstract_Char_Stack ;
S1 : Stack_Type;
S2 : Stack_Type;
Ch : Character;
-----------------------------------------------------------
package body Abstract_Char_Stack is
----------------------------------------------
procedure Push(Stack : in out Stack_Type;
Item : in Character) is
begin
Stack.Index := Stack.Index + 1;
Stack.Space(Stack.Index) := Item;
end Push;
--------------------------------------------
procedure Pop (Stack : in out Stack_Type;
Char : out Character) is
begin
Char := Stack.Space(Stack.Index);
Stack.Index := Stack.Index - 1;
end Pop;
--------------------------------------------
end Abstract_Char_Stack;
-----------------------------------------------------------
begin
Push(S1,'H'); Push(S1,'E');
Push(S1,'L'); Push(S1,'L');
Push(S1,'O'); -- S1 holds O,L,L,E,H
for I in 1..5 loop
Pop(S1, Ch);
Put(Ch); -- displays OLLEH
Push(S2,Ch);
end loop; -- S2 holds H,E,L,L,O
New_Line;
Put_Line("Order is reversed");
for I in 1..5 loop
Pop(S2, Ch);
Put(Ch); -- displays HELLO
end loop;
end Test_ADT_Stack;
-----------------------------------------------------------