From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.99.36.131 with SMTP id k125mr3886687pgk.25.1489006988556; Wed, 08 Mar 2017 13:03:08 -0800 (PST) X-Received: by 10.157.56.117 with SMTP id r50mr1263153otd.12.1489006988501; Wed, 08 Mar 2017 13:03:08 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!u69no1231995ita.0!news-out.google.com!15ni8974itm.0!nntp.google.com!w124no1232730itb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 8 Mar 2017 13:03:08 -0800 (PST) In-Reply-To: <86mvcv4zyu.fsf@gaheris.avalon.lan> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.130.162.123; posting-account=lJ3JNwoAAAAQfH3VV9vttJLkThaxtTfC NNTP-Posting-Host: 50.130.162.123 References: <86mvcv4zyu.fsf@gaheris.avalon.lan> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <50e05b28-f260-472d-9736-343fa8439613@googlegroups.com> Subject: Re: Can't access record attribute in derived type From: Shark8 Injection-Date: Wed, 08 Mar 2017 21:03:08 +0000 Content-Type: text/plain; charset=UTF-8 Xref: news.eternal-september.org comp.lang.ada:33497 Date: 2017-03-08T13:03:08-08:00 List-Id: On Wednesday, March 8, 2017 at 7:15:22 AM UTC-7, Mart van de Wege wrote: > Hi, > > I have the following definitions > > private > type Creature is new Base_Creature with record > Attributes : Attribute_Array; > Gender : Possible_Gender := Unknown; > Current_Hit_Points : Integer; > Parents : Group_Of_Creatures.List; > -- An instance of Ada.Containers.Indefinite_Doubly_Linked_Lists > end record; > > In child package #1 (Persons): > > private > type Person is new Creature with record > [...] > and in child package #2 (Knights): > type Knight is new Person with record > [...] > > I try to read the first element of the Parents attribute in knights.adb > like this: > > function Father (K : in Knight) return Knight is > begin > return K.Parents.First_Element; > end Father; One Problem is that K.Parents.First_Element *isn't* of type Knight -- It's of type Creature -- that is surely one problem. Secondly, the construct > type Creature is new Base_Creature with record > Attributes : Attribute_Array; > Gender : Possible_Gender := Unknown; > Current_Hit_Points : Integer; > Parents : Group_Of_Creatures.List; > -- An instance of Ada.Containers.Indefinite_Doubly_Linked_Lists > end record; cannot exist, as you cannot instantiate the Container without completing the type which can't be complete without the instantiation of the Container (because of the Parents element). I would recommend something like this: Type Base_Creature is abstract tagged record Attributes : Attribute_Array; Gender : Possible_Gender := Unknown; Current_Hit_Points : Integer; end record; --... Package Creature_Grouping is new Ada.Containers.Indefinite_Doubly_Linked_Lists( Base_Creature'Class ); --... Type Creature is new Base_Creature with record Parents : Creature_Grouping.List; end record; --... Type Person is new Creature with --[...] Function Father( Input : Person ) return Creature'Class is ( Input.Parents.First_Element ); --... Type Knight is new Person with --[...]