【发布时间】:2019-08-31 20:41:16
【问题描述】:
我正在尝试在 Ada 2012 中编写一个非常原始的链表示例程序。我的代码由 3 个文件组成,linked_list.adb、linked_list.ads 和 main.adb。
用户将运行程序并简单地输入一个数字序列,然后输入零来结束序列并退出。程序只是从标准输入中读取这些数字,打印出列表然后退出。
这是我的完整代码...
文件:“main.adb”
with Linked_List; use Linked_List;
procedure Main is
L : access List_Item;
begin
L := new List_Item'(null, 0);
while Append_Item (L) loop
null;
end loop;
Print_List (L);
end Main;
文件:“linked_list.ads”
with Ada.Text_IO; use Ada.Text_IO;
package Linked_List is
type List_Item is private;
function Append_Item (List_Head : access List_Item) return Boolean;
procedure Print_List (List_Head : access List_Item);
private
type List_Item is
record
Next_Item : access List_Item;
ID : Integer;
end record;
end Linked_List;
文件:“linked_list.ads”
with Ada.Text_IO; use Ada.Text_IO;
package body Linked_List is
function Append_Item (List_Head : access List_Item) return Boolean is
Runner : access List_Item := List_Head;
new_ID : Integer;
begin
if Runner.Next_Item = null then -- if we've found the last item
Put ("Enter ID for new Item (enter 0 to stop): ");
Get (new_ID);
if new_ID = 0 then
return false; -- user wants to quit
else if;
-- add a new item to the end of the list
Runner.Next_Item := new List_Item'(null, new_ID);
return true;
end if;
else;
Runner := Runner.Next_Item;
end if;
end Append_Item;
procedure Print_List (List_Head : access List_Item);
Runner : access List_Item := List_Head;
begin
if Runner = null then
return;
else;
Put ("Item ID: "); Put (Runner.ID);
Runner := Runner.Next_Item;
end if;
end Print_List;
end Linked_List;
我使用的是 Gnatmake 7.4.0,我的编译器命令行是
gnatmake -gnaty -gnaty2 -gnat12 main.adb
我看到的错误信息是:
gnatmake -gnaty -gnaty2 -gnat12 main.adb
aarch64-linux-gnu-gcc-7 -c -gnaty -gnaty2 -gnat12 main.adb
main.adb:6:22: expected private type "List_Item" defined at linked_list.ads:4
main.adb:6:22: found a composite type
gnatmake: "main.adb" compilation error
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 4
我编写的语法似乎与我试图学习的书一致:John Barnes 的“Programming in Ada 2012”。
该记录是私下声明的,因此我的客户端程序(主程序)看不到列表机制内部工作的血腥细节。我做错了什么?
【问题讨论】:
-
在我看来
new List_Item'(null, 0);正是利用了列表机制的血腥细节! -
吹毛求疵 - 列表和列表项是完全不同的东西,最好不要给它们同名
标签: ada