【问题标题】:Ada - Function within a package. Error says the package should end where the function endsAda - 包内的函数。错误说包应该在函数结束的地方结束
【发布时间】:2020-06-18 08:20:52
【问题描述】:

当我尝试运行该程序时,我收到一条错误消息,指出“end Animals;”预计在我结束函数“初始化”的地方。我对编程很陌生,我只是不知道问题出在哪里。它试图让我把它改成“终结动物”;但这将在那里结束包,而不是运行它的其余部分。

package body Animals is

   function Init(
                 Name : in String;
                 Legs : in Natural;
                 WeightInGrams : in Positive;
                 HeightInCm : in Positive)
                 return Creature;
   TempCreature : Creature;
begin
   TempCreature.Name := To_Unbounded_String(Name);
   TempCreature.Legs := Legs;
   TempCreature.WeightInGrams := WeightInGrams;
   TempCreature.HeightInCm := HeightInCm;
   return TempCreature;
end Init;                                                ***--"end Animals;" expected***

function Init return Creature is
   TempCreature : Creature;
begin
   TempCreature.Name := To_Unbounded_String("Dog");
   TempCreature.Legs := 4;
   TempCreature.WeightInGrams := 3000;
   TempCreature.HeightInCm := 40;
   return TempCreature;
end Init;

procedure Set_Legs(
                   Creat : in out Creature;
                   Legs : in Natural) is
begin
   Creat.Legs := Legs;
end Set_Legs;

procedure Set_Weight(
                     Creat : in out Creature;
                     WeightInGrams : in Positive) is
begin
   Creat.WeightInGrams := WeightInGrams;
end Set_Weight;

procedure Set_Height(
                     Creat : in out Creature;
                     HeightInCm : in Positive) is
begin
   Creat.HeightInCm := HeightInCm;
end Set_Height;

function Get_Legs(
                  Creat : in out Creature)
                  return Natural is
begin
   return Creat.Legs;
end Get_Legs;

function Get_Weight(
                    Creat : in out Creature)
                    return Positive is
begin
   return Creat.WeightInGrams;
end Get_Weight;

function Get_Height(
                    Creat : in out Creature)
                    return Positive is
begin
   return Creat.HeightInCm;
end Get_Height;

overriding procedure Finalize(
                              Creat : in out Creature) is
begin
   Put_Line("Resetting values of Creat to defaults.");
   Creat.Name := Null_Unbounded_String;
   Creat.Legs := 0;
   Creat.WeightInGrams := 1;
   Creat.HeightIncm := 1;
end Finalize;

procedure Print_Record(Creat : in out Creature) is
begin
   Private_Print_Record(Creat);
end Print_Record;

procedure Private_Print_Record(Creat : in out Creature) is
begin
   Put_Line("The animal: ");
   Put_Line("The name: " & To_String(Creat.Name));
   Put_Line("Number of legs: " & Natural'Image(Creat'Legs));
   Put_Line("Weight in grams: " & Positive'Image(Creat.WeightInGrams));
   Put_Line("Height in cm: " & Positive'image(Creat.HeightInCm));
end Private_Print_Record;
end Animals;

【问题讨论】:

  • DeeDee 已经回答了,但这里有一个提示:如果你看一下缩进,很明显有些地方不太对劲。所以你的 IDE 在这种情况下给了你一个线索

标签: function oop package ada


【解决方案1】:

您似乎在函数声明的末尾打错字了。

 function Init(
                 Name : in String;
                 Legs : in Natural;
                 WeightInGrams : in Positive;
                 HeightInCm : in Positive)
                 return Creature;            --  remove ";" add "is"
   TempCreature : Creature;
begin
   -- ...
end Init;

应该是

function Init
   (Name          : in String;
    Legs          : in Natural;
    WeightInGrams : in Positive;
    HeightInCm    : in Positive) return Creature
is
   TempCreature : Creature;
begin
   -- ...
end Init;

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2021-11-23
    • 1970-01-01
    • 2020-05-19
    • 2010-09-15
    • 2014-03-25
    • 2018-05-24
    • 2011-01-24
    • 2019-08-03
    相关资源
    最近更新 更多