如何使用用户输入变量作为泛型包的参数? [英] How to use user input variable as parameter for generic package?

查看:114
本文介绍了如何使用用户输入变量作为泛型包的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Stack.adb 中我已经指定了两个参数(Size和Type)。我想创建一个具有用户在 multistack.adb 文件中指定的确切数据类型的堆栈。



<我似乎无法找到一种方法来为堆栈类型创建一个包含用户定义变量的堆栈或实例化堆栈。在我继续之前,代码在下面(为了避免代码墙,我已经拿出了一些不相关的行):
$ b $ p $ Stack.adb

  GENERIC 
SIZE:Integer; - 堆栈的大小
TYPE项目是私有的; - 堆栈类型






multistack.adb

  WITH Ada.Text_Io;使用Ada.Text_Io; 
WITH Stack;
程序multistack是
包装是新的Ada.Text_Io.Integer_Io(整数);使用Iio;
类型StackType IS(Int,Str,Char,Day);

package stack_io是新的Ada.Text_IO.Enumeration_IO(StackType);使用stack_io;

包get_user_specs是
函数makestack返回StackType;
end get_user_specs;

包体get_user_specs是
函数makestack返回StackType是
s_type:StackType;
begin
put(什么是堆栈类型?);新队;
get(s_type);
返回s_type;
结束makestack;
begin
null;
end get_user_specs;

user_stack_type:StackType:= get_user_specs.makestack;

包装User_Stack是新堆栈(100,user_stack_type);使用User_Stack;

BEGIN
null;
END Multistack;

所以,您可以通过代码告诉我为堆栈类型创建了数据类型。我还创建了一个 Enumeration_IO 包来获得用户输入。我遇到的具体问题是:
$ b $ pre $ PACKAGE User_Stack是新堆栈(100,user_stack_type);使用User_Stack;

它抱怨我试图使用 user_stack_type 作为类型。具体错误是期望有效的子类型标记实例化Item,然后说User_Stack是未定义的。



I做了一个 put(user_stack_type)来测试,我可以确认它确实得到了用户指定的数据类型。那么为什么它不允许我创建这个包User_Stack?

解决方案

在你的片段中, user_stack_type 对象声明 ,但是 泛型实例 需要 子类型标记 。获得所需效果的一种方法是,一旦知道所选子类型,就在嵌套作用域中实例化泛型:
$ b

  if User_Stack_Type = Int然后
声明
包User_Stack是新堆栈(100,Integer);
begin
Put_Line(Stack_Type'Image(User_Stack_Type));
...
end;
结束if;


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;

这篇关于如何使用用户输入变量作为泛型包的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆