Ada 函数,访问堆栈
Ada functions, accessing stack
对 Ada 很陌生,这是我第一次用它编码。很失落。任何提示和方向都会很棒。
艾达问题:
我正在尝试制作:函数 Top (S : Stack) return Item_Type
,它 returns 堆栈中的顶部项目或引发下溢异常,到通用无界堆栈包。
我为此添加的函数位于此代码块的底部。
当前错误:
在表达式或调用中无效使用子类型标记
"From" 的实际值必须是变量
在表达式或调用中无效使用子类型标记
package body Unbound_Stack is
type Cell is record
Item : Item_Type;
Next : Stack;
end record;
procedure Push (Item : in Item_Type; Onto : in out Stack) is
begin
Onto := new Cell'(Item => Item, Next => Onto); -- '
end Push;
procedure Pop (Item : out Item_Type; From : in out Stack) is
begin
if Is_Empty(From) then
raise Underflow;
else
Item := From.Item;
From := From.Next;
end if;
end Pop;
function Is_Empty (S : Stack) return Boolean is
begin
return S = null;
end Is_Empty;
--added this code, and then had errors!
function Top (S : Stack) return Item_Type is
begin
--Raise the underflow
if Is_Empty(S) then
raise Underflow;
else
--or return top item from the stack, call pop
Pop (Item_Type, from => S);--I think I should pull from the stack w/pop
end if;
return Item_Type;
end Top;
end Unbound_Stack;
您有两条错误消息涉及此行:
Pop (Item_Type, from => S);--I think I should pull from the stack w/pop
第一个指着Item_Type
说"invalid use of subtype mark in expression or call"。
- 这意味着您在不允许的地方使用了类型的名称。子程序的实际参数永远不能是类型。您需要使用(取决于参数方向)实参的变量或表达式。
您正在将类型 (Item_Type) 传递给 Pop。相反,您需要声明一个 Item_Type 类型的变量并使用它。
例如
function Top (S : Stack) return Item_Type is
Popped_Item : Item_Type;
begin
...
然后对 Pop 的调用变为:
Pop (Item => Popped_Item, From => S)
对 Ada 很陌生,这是我第一次用它编码。很失落。任何提示和方向都会很棒。
艾达问题:
我正在尝试制作:函数 Top (S : Stack) return Item_Type
,它 returns 堆栈中的顶部项目或引发下溢异常,到通用无界堆栈包。
我为此添加的函数位于此代码块的底部。 当前错误: 在表达式或调用中无效使用子类型标记 "From" 的实际值必须是变量 在表达式或调用中无效使用子类型标记
package body Unbound_Stack is
type Cell is record
Item : Item_Type;
Next : Stack;
end record;
procedure Push (Item : in Item_Type; Onto : in out Stack) is
begin
Onto := new Cell'(Item => Item, Next => Onto); -- '
end Push;
procedure Pop (Item : out Item_Type; From : in out Stack) is
begin
if Is_Empty(From) then
raise Underflow;
else
Item := From.Item;
From := From.Next;
end if;
end Pop;
function Is_Empty (S : Stack) return Boolean is
begin
return S = null;
end Is_Empty;
--added this code, and then had errors!
function Top (S : Stack) return Item_Type is
begin
--Raise the underflow
if Is_Empty(S) then
raise Underflow;
else
--or return top item from the stack, call pop
Pop (Item_Type, from => S);--I think I should pull from the stack w/pop
end if;
return Item_Type;
end Top;
end Unbound_Stack;
您有两条错误消息涉及此行:
Pop (Item_Type, from => S);--I think I should pull from the stack w/pop
第一个指着Item_Type
说"invalid use of subtype mark in expression or call"。
- 这意味着您在不允许的地方使用了类型的名称。子程序的实际参数永远不能是类型。您需要使用(取决于参数方向)实参的变量或表达式。
您正在将类型 (Item_Type) 传递给 Pop。相反,您需要声明一个 Item_Type 类型的变量并使用它。
例如
function Top (S : Stack) return Item_Type is
Popped_Item : Item_Type;
begin
...
然后对 Pop 的调用变为:
Pop (Item => Popped_Item, From => S)