* Re: Queue System -- record type has changed?
1997-10-07 0:00 Queue System -- record type has changed? Nathan J
@ 1997-10-07 0:00 ` Matthew Heaney
0 siblings, 0 replies; 2+ messages in thread
From: Matthew Heaney @ 1997-10-07 0:00 UTC (permalink / raw)
In article <343A0322.58B4@echidna.stu.cowan.edu.au>,
fytyxt@echidna.stu.cowan.edu.au wrote:
>If this helps any, I am using GNAT ADA 3.07 to code and compile it (as
>far as I can tell)
As some point real soon, you should upgrade to the latest version of GNAT,
3.10p.
>Using a predefined Queue structure, I enqueue a few dummy records of
>type Record_Type (a few strings and integers), containing a couple of
>integers and floating point values. I then ask to call a procedure
>"Display" whose spec is:
>
> Generic
> with procedure put_object(object : object_type);
> procedure display(the_queue : queue);
> -- results : Displays the queue of objects.
This is the standard idiom for a "passive iterator" in Ada. For that
reason, I wouldn't name the generic Display, because the instantiation can
do anything, not just print out the values:
generic
with procedure Process (Object : Object_Type);
procedure For_Every_Item (Queue : Queue_Type);
>The body contains a few while and if statements but the bit I am
>concentrating on is:
>
> put_object(the_queue.objects(i));
>Where the_queue.objects(i) is number (i) of a for loop, and
>the_queue.objects is an array of record_type, which was added to when I
>enqueued the dummy records.
>
>I added a spec for put_object: (Fnord, by the way, means nothing)
>
> procedure put_object(object : object_type);
> -- fnord
Here's where you start to lose me. Where did you "add a spec"? Do you
mean the queue package itself, or in a scope where the representation of
the record is known?
Also, you don't necessarily need a spec for a subprogram. Here's what one
does normally:
package body P is
type T is
record
Part_One : Integer;
end record;
package T_Queues is new Queues_G (T);
use T_Queues;
procedure Put_Item (Item : T) is
begin
Integer_Text_IO.Put (Item.Part_One);
-- the rep of T is directly visible here, so "Item.Part_One" is OK
end;
procedure Put_Queue is
new For_Every_Item (Put_Item);
>And the body:
>
> procedure put_object(object : object_type) is
>
> package integer_io is new text_io.integer_io(integer); --
You don't need to do this, as package Integer_Text_IO is part of the
predefined environment.
>Instantiations
> package float_io is new text_io.float_io(float); -- For I/O
You don't need to do this either, because Float_Text_IO is part of the
predefined environment too.
> use float_io;
> use integer_io;
These are decidedly bad names anyway, because they're the same as the
generic packages. Best to use some other name (which is why the predefined
packages were named as the were).
>
> begin
> put (object.part_one);
>
> end put_object;
If this subprogram where declared in the body of the queue package, then of
course it would NOT work. Inside the queue package, it refers to the
generic formal parameter, not the actual parameter. So inside the queue
package, you can't refer to components of the record passed in as actual
type, because it doesn't know anything about that, only that it's a type
that has assignment.
Perhaps you already know this, but I don't want to make any assumptions.
>Where part_one is the name of the first field in the Record_Type that I
>defined, of type integer.
>
>Because this came up with some error I couldn't understand (just like
>most of them), I commented out the "Generic" and "With procedure &c."
>lines from the display spec, and re-compiled it.
>
>It refuses to recognise object.part_one, because:
>
>139. put (object.part_one);
> |
> >>> invalid prefix in selected component "temp_record"
>
>I can't understand this. As far as I can tell, the structure should be
>exactly the same as Record_type...
Then why did you use type Object_Type as the parameter name of the
subprogram used to instantiate the generic iterator? Why didn't you say
procedure Put_Object (Object : Record_Type) is
begin
Integer_Text_IO.Put (Object.Part_One);
end;
What is type Object_Type anyway?
>Finally, if I delete the "Put (object.part_one);" line from the code and
>insert:
>
> Put ("Hi");
>
>It compiles, links, and when I try to generate an EXE file, it kicks me
>into GDB (?) after creating the EXE, and if I run either the EXE file,
>or the COF through GDB, if prints "Hi" three times (because I enqueued 3
>records), and then exits normally (Checked with a "Put ("Exited
>normally")" just before the end statement)
That is prints "Hi" three times means you're interating properly. Sounds
like you have a type name problem. The parameter of the subprogram you use
to instantiate the generic passive iterator must have the same type as the
type you used to instantiate the queue package, which is the type that has
as one of its components Part_One.
I don't know why it put you into GDB though. Trying upgrading to the
latest version of GNAT (the version you're using is VERY old).
--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271
^ permalink raw reply [flat|nested] 2+ messages in thread