0

我希望你能给我建议。我第一次尝试使用 ADA 95 的面向对象功能,并且我想要两个派生类 cyclicgroup 和 polyggroup 来调用属于它们的基类 abstractGroup 的 put() 方法。但是编译器并没有像我预期的那样跟踪类层次结构,而是告诉我在对 put() 的调用中存在类型不匹配。如何告诉编译器识别对象之间的连接?

以下是 5 个文件,删除了所有无关的内容,并尝试编译:

grpdriver2.adb:

与 Ada.Text_IO;使用 Ada.Text_IO;使用 Ada.Command_Line;使用 Ada.Command_Line;

与groupstuff2;与 subgrphandler2;

procedure grpdriver2 is
    cycg:  groupStuff2.cyclicgroup;
    polyg: groupStuff2.polygonGroup;
begin

    cycg := groupstuff2.createCyclicGroup( 10);
    subgrphandler2.put(cycg);   -- line 13

    ------------------------------------------------------------------------------------------------------------------

    polyg := groupstuff2.createPolygonGroup( 10);
    subgrphandler2.put(polyg);     -- line 18

end grpdriver2;

..................................................... ..................................................... ......

groupstuff2.ads:

与 Ada.Text_IO;使用 Ada.Text_IO;使用 Ada.Command_Line;使用 Ada.Command_Line;

包 groupstuff2 是

type abstractGroup is tagged record
    x: integer;
end record;


type cyclicGroup is new abstractGroup with record
    y: integer;
end record;


function createCyclicGroup( size: in integer)  return cyclicGroup ;

----------------------------------------

type polygonGroup is new abstractGroup with record
    null;
end record;

function createPolygonGroup( size: in integer) return polygonGroup ;

端组材料2;

..................................................... .....................................

groupstuff2.adb:

包体groupstuff2是

procedure put( g: in abstractGroup) is
    x: integer;
begin
    x := 1;
end put;


function createCyclicGroup( size: in integer)  return cyclicGroup is
    cycg: cyclicGroup;
begin
    cycg.x := size;

    return cycg;

end createCyclicGroup;

function createPolygonGroup( size: in integer) return polygonGroup is
    polyg: polygonGroup;
begin
    polyg.x := size;


    return polyg;

end createPolygonGroup;

端组材料2;

..................................................... ......................

subgrphandler2.ads:

与 Ada.Text_IO;使用 Ada.Text_IO;使用 Ada.Command_Line;使用 Ada.Command_Line;

与groupstuff2;

包 subgrphandler2 是

procedure  put( g: in groupStuff2.abstractGroup);

结束子控制器2;

..................................................... .........................................................

subgrphandler2.adb:

包体 subgrphandler2 是

procedure  put( g: in groupStuff2.AbstractGroup) is

begin
        put("THIS IS A PUT STATMENT");      
end put;

结束子控制器2;

编译尝试:

C:\GNAT\2018\bin\ceblang>gnatmake grpdriver2 gcc -c grpdriver2.adb grpdriver2.adb:13:36:预期类型“abstractGroup”定义在 groupstuff2.ads:7 grpdriver2.adb:13:36:找到类型“ cyclicGroup”定义在 groupstuff2.ads:16 grpdriver2.adb:18:36:预期类型“abstractGroup”定义在 groupstuff2.ads:7 grpdriver2.adb:18:36:发现类型“polygonGroup”定义在 groupstuff2.ads:25 gnatmake :“grpdriver2.adb”编译错误

4

1 回答 1

0

我无法编译 Ada95 代码,因为我使用的是 GNAT CE 2018(仅支持 Ada 2012),但您似乎需要将Class属性添加到 in 的参数类型中putsubgrphandler2以使其接受类范围的类型(即groupStuff2.AbstractGroup,及其所有扩展(继承类型))。

您可能还想通过将关键字添加到其定义中来使其groupStuff2.AbstractGroup真正抽象(见下文)。abstract

这(重新格式化的代码)在 GNAT CE 2018 上以 Ada 2012 模式编译:

group_driver_2.adb

with Group_Stuff_2; 
with Sub_Group_Handler_2;

procedure Group_Driver_2 is

   Cycg  : Group_Stuff_2.Cyclic_Group;
   Polyg : Group_Stuff_2.Polygon_Group;

begin

   Cycg := Group_Stuff_2.Create_Cyclic_Group (10);
   Sub_Group_Handler_2.Put (Cycg);

   Polyg := Group_Stuff_2.Create_Polygon_Group (10);
   Sub_Group_Handler_2.Put (Polyg);

end Group_Driver_2;

group_stuff_2.ads

package Group_Stuff_2 is

   type Abstract_Group is abstract tagged 
      record
         X: Integer;
      end record;

   --  Cyclic_Group 

   type Cyclic_Group is new Abstract_Group with 
      record
         Y: Integer;
      end record;

   function Create_Cyclic_Group
     (Size: in Integer)  return Cyclic_Group;

   --  Polygon_Group 

   type Polygon_Group is new Abstract_Group with null record;

   function Create_Polygon_Group
     (Size: in Integer) return Polygon_Group ;

end Group_Stuff_2;

group_stuff_2.adb

package body Group_Stuff_2 is

   -------------------------
   -- Create_Cyclic_Group --
   -------------------------

   function Create_Cyclic_Group
     (Size : in Integer) return Cyclic_Group
   is
      Cycg : Cyclic_Group;
   begin
      Cycg.X := Size;
      return Cycg;
   end Create_Cyclic_Group;

   --------------------------
   -- Create_Polygon_Group --
   --------------------------

   function Create_Polygon_Group
     (Size : in Integer) return Polygon_Group 
   is
      Polyg: Polygon_Group;
   begin
      Polyg.X := Size;
      return Polyg;
   end Create_Polygon_Group;

end Group_Stuff_2;

sub_group_handler_2.ads

with Group_Stuff_2;

package Sub_Group_Handler_2 is

   procedure Put (G : in Group_Stuff_2.Abstract_Group'Class);

end Sub_Group_Handler_2;

sub_group_handler_2.adb

with Ada.Text_IO; use Ada.Text_IO;

package body Sub_Group_Handler_2 is

   procedure Put (G: in Group_Stuff_2.Abstract_Group'Class) is
   begin
      Put_Line ("Value of X is" & Integer'Image (G.X));      
   end Put;

end Sub_Group_Handler_2;
于 2019-04-21T08:52:13.653 回答