* HELP ! Problems with Get and Put in Generic Procedure
@ 2003-07-10 22:01 Tanya
2003-07-10 22:37 ` Robert I. Eachus
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Tanya @ 2003-07-10 22:01 UTC (permalink / raw)
I am having problems compiling a program that uses a generic GetData
procedure. Inside this procedure I try to Get data from the keyboard
and store into an array. My generic specification and body both
compile fine...but when I try to instantiate the generic procedure in
the main driver, I get errors:
"procedure or entry name expected" and "expect valid subprogram name
in renaming". What am I doing wrong ?? I can't find any examples of
how to do this. Any help would be greatly appreciated !!! Thanks !
Here is my program:
(generic spec file) ******************************************
GENERIC
TYPE ElementType IS PRIVATE; -- any nonlimited type
TYPE IndexType IS ( <> ); -- any discrete type for
-- index value
TYPE ArrayType IS ARRAY (IndexType RANGE <>) OF ElementType;
WITH PROCEDURE Read (Item : IN ElementType);
PROCEDURE Get_Array_Generic (List : IN OUT ArrayType);
-- Input : Array type and input file stream.
-- Action: Will read in elements and store in array.
-- Output: Array filled with elements.
(generic body file) ********************************************
PROCEDURE Get_Array_Generic (List : IN OUT ArrayType) IS
-------------------------------------------------------------------
-- Input : Array type and input file stream.
-- Action: Will read in elements and store in array.
-- Output: Array filled with elements.
-------------------------------------------------------------------
Index : IndexType;
Element : ElementType;
BEGIN -- Get_Array_Generic
Index := List'Last; -- initialize array index to
-- first subscript
FOR Index IN List'RANGE LOOP
Read (Item => Element); -- read elements into array
-- store data into the array
List (Index) := Element;
END LOOP;
END Get_Array_Generic;
(Main driver ) ********************************************
WITH Ada.Text_IO;
USE Ada.Text_IO;
WITH Ada.Integer_Text_IO;
USE Ada.Integer_Text_IO;
PROCEDURE Generic_Test IS
----------------------------------------------------------------------
-- Input : None
-- Action:
-- Output: None
----------------------------------------------------------------------
MaxInt : CONSTANT Integer := 10;
SUBTYPE IntegerRange IS Integer RANGE 1..MaxInt;
TYPE Integer_Array IS ARRAY (IntegerRange RANGE <>) OF Integer;
*** This is where I am having problems (in the instantiation) ***
PROCEDURE GetIntegers IS NEW Get_Array_Generic
(ElementType => Integer,
IndexType => IntegerRange,
ArrayType => Integer_Array,
Read => Ada.Integer_Text_IO);
IntArray : Integer_Array(IntegerRange);
TargetInteger : Integer;
BEGIN -- Generic_Test
Ada.Text_IO.Put (Item => "Please enter ");
Ada.Integer_Text_IO.Put (Item => MaxInt, Width => 2);
Ada.Text_IO.Put (Item => " integer values: ");
Ada.Text_IO.New_Line;
GetIntegers (List => IntArray);
END Generic_Test;
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: HELP ! Problems with Get and Put in Generic Procedure
2003-07-10 22:01 HELP ! Problems with Get and Put in Generic Procedure Tanya
@ 2003-07-10 22:37 ` Robert I. Eachus
2003-07-11 1:59 ` Jeffrey Carter
2003-07-15 1:40 ` Richard Riehle
2 siblings, 0 replies; 4+ messages in thread
From: Robert I. Eachus @ 2003-07-10 22:37 UTC (permalink / raw)
Tanya wrote:
> I am having problems compiling a program that uses a generic GetData
> procedure. Inside this procedure I try to Get data from the keyboard
> and store into an array. My generic specification and body both
> compile fine...but when I try to instantiate the generic procedure in
> the main driver, I get errors:
> "procedure or entry name expected" and "expect valid subprogram name
> in renaming". What am I doing wrong ?? I can't find any examples of
> how to do this. Any help would be greatly appreciated !!! Thanks !
>
> Here is my program:
>
> (generic spec file) ******************************************
>
> GENERIC
>
> TYPE ElementType IS PRIVATE; -- any nonlimited type
> TYPE IndexType IS ( <> ); -- any discrete type for
> -- index value
> TYPE ArrayType IS ARRAY (IndexType RANGE <>) OF ElementType;
>
> WITH PROCEDURE Read (Item : IN ElementType);
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType);
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
>
>
> (generic body file) ********************************************
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType) IS
> -------------------------------------------------------------------
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
> -------------------------------------------------------------------
>
> Index : IndexType;
> Element : ElementType;
>
> BEGIN -- Get_Array_Generic
>
> Index := List'Last; -- initialize array index to
> -- first subscript
>
> FOR Index IN List'RANGE LOOP
>
> Read (Item => Element); -- read elements into array
>
> -- store data into the array
>
> List (Index) := Element;
>
> END LOOP;
>
> END Get_Array_Generic;
>
>
> (Main driver ) ********************************************
>
>
> WITH Ada.Text_IO;
> USE Ada.Text_IO;
> WITH Ada.Integer_Text_IO;
> USE Ada.Integer_Text_IO;
>
>
> PROCEDURE Generic_Test IS
> ----------------------------------------------------------------------
> -- Input : None
> -- Action:
> -- Output: None
> ----------------------------------------------------------------------
>
> MaxInt : CONSTANT Integer := 10;
> SUBTYPE IntegerRange IS Integer RANGE 1..MaxInt;
> TYPE Integer_Array IS ARRAY (IntegerRange RANGE <>) OF Integer;
>
>
> *** This is where I am having problems (in the instantiation) ***
>
>
> PROCEDURE GetIntegers IS NEW Get_Array_Generic
> (ElementType => Integer,
> IndexType => IntegerRange,
> ArrayType => Integer_Array,
> Read => Ada.Integer_Text_IO);
-- This is the problem. You need a procedure here not a package name.
>
> IntArray : Integer_Array(IntegerRange);
>
> TargetInteger : Integer;
>
> BEGIN -- Generic_Test
>
> Ada.Text_IO.Put (Item => "Please enter ");
> Ada.Integer_Text_IO.Put (Item => MaxInt, Width => 2);
> Ada.Text_IO.Put (Item => " integer values: ");
> Ada.Text_IO.New_Line;
>
> GetIntegers (List => IntArray);
>
> END Generic_Test;
But Read => Ada.Integer_Text_IO.Read won't work because the parameter
profile doesn't match Get or Put. You can change your generic to match,
or put a "wrapper" around Ada.Text_IO.Get. (But remember that in and
out are part of the profile.)
For those of you who worry about helping students too much, I think this
is a tricky enough issue to justify this amount of help. (I suspect
that Tanya first tried with Read => Ada.Integer_Text_IO.Get.) If this
doesn't get Tanya over the hump, she can ask her instructor or TA.
--
Robert I. Eachus
�In an ally, considerations of house, clan, planet, race are
insignificant beside two prime questions, which are: 1. Can he shoot? 2.
Will he aim at your enemy?� -- from the Laiden novels by Sharon Lee and
Steve Miller.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: HELP ! Problems with Get and Put in Generic Procedure
2003-07-10 22:01 HELP ! Problems with Get and Put in Generic Procedure Tanya
2003-07-10 22:37 ` Robert I. Eachus
@ 2003-07-11 1:59 ` Jeffrey Carter
2003-07-15 1:40 ` Richard Riehle
2 siblings, 0 replies; 4+ messages in thread
From: Jeffrey Carter @ 2003-07-11 1:59 UTC (permalink / raw)
Tanya wrote:
R. Eachus addressed your question, but there's another likely
misunderstanding on your part I'd like to mention.
>
> (generic spec file) ******************************************
>
> GENERIC
>
> TYPE ElementType IS PRIVATE; -- any nonlimited type
> TYPE IndexType IS ( <> ); -- any discrete type for
> -- index value
> TYPE ArrayType IS ARRAY (IndexType RANGE <>) OF ElementType;
>
> WITH PROCEDURE Read (Item : IN ElementType);
Mode "in" seems odd for a procedure named Read.
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType);
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
>
>
> (generic body file) ********************************************
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType) IS
> -------------------------------------------------------------------
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
> -------------------------------------------------------------------
>
> Index : IndexType;
> Element : ElementType;
>
> BEGIN -- Get_Array_Generic
>
> Index := List'Last; -- initialize array index to
> -- first subscript
This assignment is the only place this variable is mentioned in this
procedure. Since you're not doing anything with it, why not eliminate it?
>
> FOR Index IN List'RANGE LOOP
>
> Read (Item => Element); -- read elements into array
>
> -- store data into the array
>
> List (Index) := Element;
>
> END LOOP;
>
> END Get_Array_Generic;
Your code will look better to most of us if you use all lower case for
reserved words, and use underlines instead of case to separate words in
identifiers (Index_Type, not IndexType).
--
Jeff Carter
"It's all right, Taggart. Just a man and a horse being hung out there."
Blazing Saddles
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: HELP ! Problems with Get and Put in Generic Procedure
2003-07-10 22:01 HELP ! Problems with Get and Put in Generic Procedure Tanya
2003-07-10 22:37 ` Robert I. Eachus
2003-07-11 1:59 ` Jeffrey Carter
@ 2003-07-15 1:40 ` Richard Riehle
2 siblings, 0 replies; 4+ messages in thread
From: Richard Riehle @ 2003-07-15 1:40 UTC (permalink / raw)
I am including Tanya's message at the end of this reply.
It is usually a design error to include the I/O capabilities in
a generic component. It is a matter of loose coupling. Separate
out the I/O portion of your design from the generic component. Add
iterators to the component if you to traverse the entire entity. You
can also add a current_item indicator to your component so you will
be able to write a method to fill and clear the array one item at a
time with an external procedure.
Richard Riehle
----------------------------------------------------------------------------
Tanya wrote:
> I am having problems compiling a program that uses a generic GetData
> procedure. Inside this procedure I try to Get data from the keyboard
> and store into an array. My generic specification and body both
> compile fine...but when I try to instantiate the generic procedure in
> the main driver, I get errors:
> "procedure or entry name expected" and "expect valid subprogram name
> in renaming". What am I doing wrong ?? I can't find any examples of
> how to do this. Any help would be greatly appreciated !!! Thanks !
>
> Here is my program:
>
> (generic spec file) ******************************************
>
> GENERIC
>
> TYPE ElementType IS PRIVATE; -- any nonlimited type
> TYPE IndexType IS ( <> ); -- any discrete type for
> -- index value
> TYPE ArrayType IS ARRAY (IndexType RANGE <>) OF ElementType;
>
> WITH PROCEDURE Read (Item : IN ElementType);
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType);
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
>
> (generic body file) ********************************************
>
> PROCEDURE Get_Array_Generic (List : IN OUT ArrayType) IS
> -------------------------------------------------------------------
> -- Input : Array type and input file stream.
> -- Action: Will read in elements and store in array.
> -- Output: Array filled with elements.
> -------------------------------------------------------------------
>
> Index : IndexType;
> Element : ElementType;
>
> BEGIN -- Get_Array_Generic
>
> Index := List'Last; -- initialize array index to
> -- first subscript
>
> FOR Index IN List'RANGE LOOP
>
> Read (Item => Element); -- read elements into array
>
> -- store data into the array
>
> List (Index) := Element;
>
> END LOOP;
>
> END Get_Array_Generic;
>
> (Main driver ) ********************************************
>
> WITH Ada.Text_IO;
> USE Ada.Text_IO;
> WITH Ada.Integer_Text_IO;
> USE Ada.Integer_Text_IO;
>
> PROCEDURE Generic_Test IS
> ----------------------------------------------------------------------
> -- Input : None
> -- Action:
> -- Output: None
> ----------------------------------------------------------------------
>
> MaxInt : CONSTANT Integer := 10;
> SUBTYPE IntegerRange IS Integer RANGE 1..MaxInt;
> TYPE Integer_Array IS ARRAY (IntegerRange RANGE <>) OF Integer;
>
>
> *** This is where I am having problems (in the instantiation) ***
>
> PROCEDURE GetIntegers IS NEW Get_Array_Generic
> (ElementType => Integer,
> IndexType => IntegerRange,
> ArrayType => Integer_Array,
> Read => Ada.Integer_Text_IO);
>
>
> IntArray : Integer_Array(IntegerRange);
>
> TargetInteger : Integer;
>
> BEGIN -- Generic_Test
>
> Ada.Text_IO.Put (Item => "Please enter ");
> Ada.Integer_Text_IO.Put (Item => MaxInt, Width => 2);
> Ada.Text_IO.Put (Item => " integer values: ");
> Ada.Text_IO.New_Line;
>
> GetIntegers (List => IntArray);
>
> END Generic_Test;
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-07-15 1:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-10 22:01 HELP ! Problems with Get and Put in Generic Procedure Tanya
2003-07-10 22:37 ` Robert I. Eachus
2003-07-11 1:59 ` Jeffrey Carter
2003-07-15 1:40 ` Richard Riehle
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox