ada - 声明必须在开始之前到来吗?

时间:2015-03-12 02:55:35

标签: ada

问题:为什么以及如何修复有关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;

1 个答案:

答案 0 :(得分:1)

正如错误消息所述,声明必须在begin之前。因此,如果您有一个声明,它必须位于包含它的构造的begin之前。例如,ixp的类型声明(不应该是类型声明,但这是另一个问题)直接包含在evaluate函数中。因此,它们必须出现在begin函数的evaluate之前。

问题在于,这也适用于functionprocedure声明 - 并且完整的函数体(例如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; 的语句,这就是为什么我不确定您是否理解这个概念。