我希望你能给我建议。我试图首次使用ADA 95的面向对象功能,并且我希望两个派生类cyclicgroup和polyggroup调用属于其基类的put()方法, abstractGroup。但是编译器并没有像我期望的那样跟踪类层次结构,只是告诉我在对put()的调用中类型不匹配。如何告诉编译器识别对象之间的连接?
这里有5个文件,其中所有多余的东西都已删除,并尝试编译:
grpdriver2.adb:
与Ada.Text_IO;使用Ada.Text_IO; 与Ada.Command_Line;使用Ada.Command_Line;
with 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 ;
end groupstuff2;
............................................... ........................................
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;
end groupstuff2;
............................................... ...............................
subgrphandler2.ads:
与Ada.Text_IO;使用Ada.Text_IO; 与Ada.Command_Line;使用Ada.Command_Line;
with groupstuff2;
包subgrphandler2是
procedure put( g: in groupStuff2.abstractGroup);
end subgrphandler2;
............................................... ............................................
subgrphandler2.adb:
包主体subgrphandler2为
procedure put( g: in groupStuff2.AbstractGroup) is
begin
put("THIS IS A PUT STATMENT");
end put;
end subgrphandler2;
比较尝试:
C:\ GNAT \ 2018 \ bin \ ceblang> gnatmake grpdriver2 gcc -c grpdriver2.adb grpdriver2.adb:13:36:在groupstuff2.ads:7处定义的预期类型“ abstractGroup” grpdriver2.adb:13:36:找到在groupstuff2.ads:16处定义的类型“ cyclicGroup” grpdriver2.adb:18:36:在groupstuff2.ads:7处定义的预期类型“ abstractGroup” grpdriver2.adb:18:36:找到在groupstuff2.ads:25处定义的类型“ polygonGroup” gnatmake:“ grpdriver2.adb”编译错误
答案 0 :(得分:0)
我无法使用GNAT CE 2018(仅支持Ada 2012)来编译Ada95代码,但是似乎您需要将Class
属性添加到{{1}的参数类型中} put
中的}使其接受类宽类型(即subgrphandler2
及其所有扩展名(继承的类型))。
您可能还想通过在其定义中添加groupStuff2.AbstractGroup
关键字来使groupStuff2.AbstractGroup
实际上是抽象的。
此(重新格式化的代码)在ANAT 2012模式下的GNAT CE 2018上编译:
group_driver_2.adb
abstract
group_stuff_2.ads
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.adb
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;
sub_group_handler_2.ads
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.adb
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;