问题:为什么以及如何修复有关ADA的错误消息? Thax
12:04声明必须在"开始"之前 预期29:01声明
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
With Ada.Strings.Unbounded; Use Ada.Strings.Unbounded;
With Ada.Strings.Bounded;
procedure polynomial is
begin
function evaluate (x: Integer) return Integer is
type i is new Integer;
type p is new Integer;
type x is new Integer;
begin
for i in reverse 0..10 loop
i:= i-1;
p:= coef(i)+x*p;
end loop;
return p;
end evaluate;
end polynomial;
答案 0 :(得分:1)
正如错误消息所述,声明必须在begin
之前。因此,如果您有一个声明,它必须位于包含它的构造的begin
之前。例如,i
,x
和p
的类型声明(不应该是类型声明,但这是另一个问题)直接包含在evaluate
函数中。因此,它们必须出现在begin
函数的evaluate
之前。
问题在于,这也适用于function
和procedure
声明 - 并且完整的函数体(例如evaluate
)被视为此规则的声明。 evaluate
直接包含在polynomial
中。因此,它必须出现在属于begin
的{{1}}行之前,而您的代码会将其放在polynomial
行之后,这是非法的。
实际上有两种解决方法:(1)在begin
行之前移动evaluate
。 (2)添加以begin
开头的块:
declare
现在procedure polynomial is
begin
declare
function evaluate (x: Integer) return Integer is
i : Integer;
-- etc.
begin
-- etc.
end evaluate;
begin -- this is the BEGIN line of the block
-- Now put some code here!
end; -- this is the end of the block
end polynomial;
函数直接包含在块中,而不是evaluate
中,它必须出现在块的polynomial
行之前,但不一定要发生在begin
的{{1}}行之前。
其他几件事:本声明:
begin
不表示“polynomial
的类型是整数”。这意味着您要声明一个名为type i is new Integer;
的新类型。你不想要那个,你想要一个变量。
i
第二:注意我说“现在把一些代码放在这里”的块的部分。这是必要的。声明函数i
不会运行该函数。如果你想让你的函数运行,你必须输入一个调用它的语句。您原来的错误代码没有任何调用i : Integer;
的语句,这就是为什么我不确定您是否理解这个概念。