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,LOTS_OF_MONEY 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-13 15:10:02 PST Path: supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-out.usenetserver.com!newsfeed.icl.net!diablo.netcom.net.uk!netcom.net.uk!not-for-mail From: "Ayende Rahien" Newsgroups: comp.lang.ada Subject: Re: Problem trying to implement generics. Date: Sat, 14 Apr 2001 01:03:42 +0200 Organization: (Posted via) GTS Netcom - Public USENET Service http://pubnews.netcom.net.uk Sender: ayende@softhome.net Message-ID: <9b7tce$laf$2@taliesin.netcom.net.uk> References: <9b46dr$cd8$1@taliesin.netcom.net.uk> <9b6jtu$4is$2@taliesin.netcom.net.uk> <9b6m27$68e$1@taliesin.netcom.net.uk> <0JBB6.10484$FD1.1197250@news6-win.server.ntlworld.com> NNTP-Posting-Host: diup-180-204.inter.net.il X-Trace: taliesin.netcom.net.uk 987199695 21839 213.8.180.204 (13 Apr 2001 22:08:15 GMT) X-Complaints-To: abuse@corp.netcom.net.uk NNTP-Posting-Date: Fri, 13 Apr 2001 22:08:15 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.60.2296.0000 X-MimeOLE: Produced By Microsoft MimeOLE V5.60.2296.0000 Xref: supernews.google.com comp.lang.ada:6875 Date: 2001-04-14T01:03:42+02:00 List-Id: "chris.danx" wrote in message news:0JBB6.10484$FD1.1197250@news6-win.server.ntlworld.com... > > > > > > > > private > > > > type Binary_Tree is new Ada.Finalization.controlled with record > > > > root: Node_access := null; > > > > end record; > > > > > > the error comes because > > > > > > > package body binTree is > > > [snip] > > > > procedure insert(insert_this : in data_type; Sort : in sort_by) is > > > > New_node : Node_Access := new node; > > > > current : Node_access := root; -- This is the line it compline > about!! > > > > > > you've forgot to that Root is in Binary_Tree. > > > > Okay, so how do I fix it? And what is wrong, anyway? > > What you do is pass in a Binary_Tree type with appropriate mode. In C++ you > have objects that store the methods with the data in the class. This means > you don't need to refer to the object name and you can just use "root". In > Ada methods are simply procedures and functions that work with objects > passed in. > > In C++ you'd have > > BinTree.insert(insert_this, sort); > > but in Ada > > insert(BinTree, insert_this, sort); > > You use the methods with the object type. So you'd have to pass in an item > of type Binary_Tree with appropriate mode and use the "root" field in this. > > e.g. > procedure insert (BT : in out Binary_Tree; > insert_this : ... > sort : ...) is > New_node : Node_Access := new node; > current : Node_access := BT.root; -- This is what you need to do! > begin > ......................... > ......................... > end insert; > > If your interested i have a generic doubly linked list ADT written in Ada. > It's part of the Heather modules i'm writing. I know it isn't based on > tagged types(Ada objects), but it does show how to build an ADT. Your > implementation of Binary_Tree is "Limited controlled" which means it's a > tagged type, but i haven't added this property to it yet. I may soon, or at > least make it controlled. It really doesn't matter that much since it's > just an example. > > I will put it up on my site if you let me know you want it. Maybe later, right now I want to implement those myself, it's a good practice, and make for excellent tools afterward. > > > I have a question. From your code it doesn't look like an ADT. Is this > > > what you intended? I can't remember the term for what this looks like, > > but > > > I think it's to do with storing the Tree as a global variable in the > > package > > > not as an ADT. > > > > Okay, major cocept problem here. > > I'm coming here with C/C++ background, and I understand that Ada has a > > different OO paradigm. > > I assumed that for each Binary_Tree spesific type that I create, there > would > > be its own Root. > > I mean: > > package Int_BinTree is new Binary_Tree(data_type=> > > Integer,Sort_by=>Integer); > > > > Bin_Tree_First, Bin_Tree_Second : Int_BinTree; > > > > Does Bin_Tree_First.Root = Bin_Tree_Second.Root ? > > > > > > > If you want an ADT you need to pass in a Binary_Tree type as necessary. > I > > > didn't see it any where in your code so i'm assuming it's based on a > > global > > > variable. > > > > I think that you are talking about ADO vs ADT , right? > > generic abstract data type & generic abstract data object > > Yes, that's it. ADO -- i must remember that! > > > > > There's nothing wrong doing it this way! > > > > Then why doesn't it compile? > > It doesn't compile because Ada's OO is based on extensible records. You > have to pass the record in even though it's an Object. In C++ the object > methods are held inside the class which means they have access to the > variables inside. This is not the same as Ada. > > Maybe, this is a better explanation, it's just like C records. Just pass in > the record and use it as you wish. Remember to get the correct mode, and > use the record like you would in C. > > There's more to Ada's OO but you don't need it for your Binary_Tree and I'm > still learning about it myself. Thanks, that helped, it now compiles. BTW, is there any reason why I can't use out parameter with a function?