In Stack.adb
I have specified two parameters (Size and Type). I want to create a stack that is of the exact data type that a user specifies within my multistack.adb
file.
I can't seem to find a way to create a new package or instantiation of a stack with a user-defined variable for the stack type. Before I go on, code is below (To avoid a wall of code, I have taken out some unrelated lines):
Stack.adb
:
GENERIC
SIZE : Integer; --size of stack
TYPE Item IS PRIVATE; --type of stack
multistack.adb
:
WITH Ada.Text_Io; USE Ada.Text_Io;
WITH Stack;
PROCEDURE multistack IS
PACKAGE Iio IS NEW Ada.Text_Io.Integer_Io(Integer); USE Iio;
Type StackType IS (Int, Str, Char, Day);
package stack_io is new Ada.Text_IO.Enumeration_IO(StackType); use stack_io;
package get_user_specs is
function makestack return StackType;
end get_user_specs;
package body get_user_specs is
function makestack return StackType is
s_type : StackType;
begin
put("What is the stack type?"); new_line;
get(s_type);
return s_type;
end makestack;
begin
null;
end get_user_specs;
user_stack_type : StackType := get_user_specs.makestack;
PACKAGE User_Stack IS NEW Stack(100, user_stack_type); use User_Stack;
BEGIN
null;
END Multistack;
So, as you can tell by the code I have created the Data type for Stack types. I also created an Enumeration_IO
package to be able to get the user input. The line I'm having specific trouble with is:
PACKAGE User_Stack IS NEW Stack(100, user_stack_type); use User_Stack;
It is complaining about the fact that I'm attempting to use user_stack_type
as the type. The specific error is expect valid subtype mark to instantiate "Item"
, then says that User_Stack is undefined.
I did a put(user_stack_type)
just to test, and I can confirm that it does get the user specified data type. So why will it not allow me to create this package User_Stack?
In your fragment, user_stack_type
is an object declaration, but a generic instantiation requires a subtype mark. One way to get the desired effect is to instantiate the generic in a nested scope once the chosen subtype is known:
if User_Stack_Type = Int then
declare
package User_Stack is new Stack(100, Integer);
begin
Put_Line(Stack_Type'Image(User_Stack_Type));
…
end;
end if;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With