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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ad988eb0a9545c86 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-04-12 11:32:02 PST Path: supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!hammer.uoregon.edu!skates!not-for-mail From: Stephen Leake Newsgroups: comp.lang.ada Subject: Re: Problem trying to implement generics. Date: 12 Apr 2001 14:06:33 -0400 Organization: NASA Goddard Space Flight Center Message-ID: References: <9b1oe1$i4m$1@taliesin.netcom.net.uk> NNTP-Posting-Host: anarres.gsfc.nasa.gov Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: skates.gsfc.nasa.gov 987099677 7317 128.183.220.71 (12 Apr 2001 18:21:17 GMT) X-Complaints-To: dscoggin@cne-odin.gsfc.nasa.gov NNTP-Posting-Date: 12 Apr 2001 18:21:17 GMT User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.6 Xref: supernews.google.com comp.lang.ada:6831 Date: 2001-04-12T18:21:17+00:00 List-Id: "Ayende Rahien" writes: > I've just started to learn Ada, and I've some problem solving some problems. Congratulations; you are learning the best language around :). > > > Here is the spec I'm trying to compile: > > with Ada.Finalization; > generic > type data_type is private; > type Sort_by is private; This is part of your problem. A private type does not have ">" defined, so you can't use ">" in the body of this package (that explains one of the compiler errors). You have a couple choices: 1) change this to "type Sort_by is new integer" This is ok, but not very flexible. Suppose you want to sort by strings? 2) Replace with a function: with function Greater_Than (Left, Right : in Data_Type) return Boolean; Now, in the body, call "Greater_Than (Data_1, Data_2)". > package Binary_Trees is > type binary_Tree is new Ada.Finalization.controlled with private; > type node is private; > type node_access is access all Node; > type data_access is access all Data_type; > procedure insert(insert_this : in data_type; Sort : in sort_by); > --Insert into the binary tree, sort according to sort. > function Delete(Delete_this: in Sort_by; Index : in natural := 1) return > boolean; > -- Delete a node from the tree, return true if succeeded, return false if > -- the node does not exist. > function Get(Search_For: in sort_by; index : in natural := 1) return > data_access; > -- Search for nodes with duplicate sort_by variable. It find the first > -- node with the sort_by equal to the given one, and then continue to check > for Index > -- number of duplicated. It return null if it there isn't a suitable node > in the > -- correct index. > private > type Binary_Tree is new Ada.Finalization.controlled with record > root: Node_access := null; > end record; > type node is record > Data : Data_type; > Sorted_by: Sort_by; > Parent, Left, right : Node_access; > end record; > procedure Adjust(Object : in out Binary_Tree ); > procedure Finalize(Object : in out Binary_Tree ); > end Binary_Trees; > > I trying to put the defination of Node in the body, but it results in > compilation error, is there some way to put the defination in the > body? Yes; you use an "incomplete type declaration": package A_Package is private type Node_Type; type Node_Access_Type is access all Node_Type; end A_Package; package body A_Package is type Node_Type is record A : Integer; end record; end A_Package; Note that the incomplete type must be declared in the private part. Since you are referencing Node_Type in functions in the public part, this won't work for you. > does it matter naught, because it's on the private part of the > package? Users cannot see the "insides" of Node, so you still have data hiding and abstraction. Moving Node to the body would let you change its "insides" without recompiling. Sometimes that is important in a large project; not here. You could probably benefit from a good example; see my binary tree package at http://users.erols.com/leakstan/Stephe/Ada/sal.html But don't give up on yours; making your own binary tree is a good way to learn! -- -- Stephe