- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 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;
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;
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 -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
最佳答案
原因是:type List_Item is private
(来自 Ada 代码的简单英语)!
这意味着包 Linked_List 的作者不希望它的用户使用它的详细信息(它是一个包含两个组件的记录)。在更复杂的软件中,隐藏这些细节很有用,因为它们可能会发生变化,如果用户使用在 List_Item 类型的设计更改后变得不兼容的细节(在这种情况下,复合类型),用户会遇到麻烦.
有两种解决方案:
Null_List : constant List_Item;
后 type List_Item is private
和 Null_List : constant List_Item := (null, 0);
在私有(private)部分。那么你可以使用Null_List
在您的 main.adb 中。 关于ada - (Ada 2012) 编译时错误 "expected private type... found composite type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57593690/
如何在过程中编写迭代器?对不起我的转储问题,我是新手。感谢您的回答。 最佳答案 这完全取决于您需要迭代的内容。 数组?使用loop : plain, for, or while. predefined
我现在知道很多编程语言。回到我 18 岁的时候,我几乎加入了美国空军,并且对 Ada 进行了测试。那是十多年前的事了。 Ada 编程语言在军队中是否仍然像以前一样重要? 我想知道新的军事软件项目是否仍
在 Java 或 C# 中,您经常会拥有 final 的类成员。或 readonly - 它们设置一次,然后再也不碰。它们可以为类的不同实例保存不同的值。 艾达有没有类似的东西?我试图在 Ada 中创
即使使用这个简单的示例,我也无法让动态调度正常工作。我相信问题在于我如何设置类型和方法,但看不到在哪里! with Ada.Text_Io; procedure Simple is type A
我目前正在自学 Ada,尽管我可以从解决一些更传统的问题开始。 更具体地说,我尝试计算阶乘 n!,而 n>100。到目前为止,我的实现是: with Ada.Text_IO; with Ada.Int
目前正在学习 Ada 并真正享受它,有一件事情困扰着我:什么是 tagged类型?根据 John Barnes 的 Programming in Ada 2012,它表示实例化的对象在运行时带有标签。
你好 我正在尝试我在 Ada 中创建单人骰子游戏的第一个程序。 但面临着保持球员得分的问题。 目标:每个玩家有 10 个回合,如果 2 次掷骰总数为 7,则获得 10 分 问题:每次总分被重置并且 1
您可以通过让函数返回一个值来分配给变量: My_Int : Integer := My_Math_Func [(optional params)]; 或者你可以用一个过程来做到这一点(假设 My_In
我试图在 Ada 中将字符转换为整数,似乎没有任何效果,到目前为止我已经能够从 ASCII 返回 DEC,但我想返回 0(整数)。 Character'Pos('0'); 返回 48 --我希望它返回
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我正在写一篇关于 Ada 83 的论文。我们有一个作业,列出了论文的各个部分(历史、设计目标、语法等)。讲师提到我们中的一些人将有一些部分简单地说“此语言不支持此功能。” 其中两个部分是数据类型和
假设我有以下常量来定义一个只接受其范围定义内的有效值的子类型: type Unsigned_4_T is mod 2**4; valid_1 : constant Unsigned_4_T :=
我正在尝试创建一个 tree-sitter解析器,以便 IDE(在本例中为 Vim)可以解析 Ada 程序文本并进行更高级的操作,例如 extract-subprogram 和 rename-vari
我想声明一个元素类型为变体记录的数组。像这样: type myStruct (theType : vehicleType) is record ... when car => numOfWheels
我正在实例化一个带有枚举的通用包,以访问多个值之一并在子程序重载中使用。我想要一组定义明确、编译时检查过的值,我可以使用和查找。 generic -- Different types beca
我有以下包: ------------------- -- File: father.ads ------------------- package Father with SPARK_Mode =>
对于最后的程序,我从 gnat 收到以下错误消息: test2.adb:23:61: error: invalid operand types for operator "-" test2.adb:2
我编写了一个加密文件的 Ada 程序。它逐 block 读取它们以节省目标机器上的内存。不幸的是,Ada 的目录库读取 Long_Integer 中的文件大小,将读取限制为近 2GB 文件。尝试读取超
我想打印访问变量(指针)的地址以进行调试。 type Node is private; type Node_Ptr is access Node; procedure foo(n: in out No
我是一名优秀的程序员,十分优秀!