使用函数的返回值在通用包体中进行变量初始化 [英] Variable initialization in generic package body using return value of function
问题描述
GENERIC
TYPE项目是PRIVATE; - 数组的类型
size:integer; - 数组的大小
包gwar IS
函数get_size返回整数;
END gwar;
以及正文档:
与Ada.Text_Io;
使用Ada.Text_Io;
包体gwar是
--Get_Size允许txt文件指定分配多少空间。
函数get_size返回整数是
文件名:字符串:=win.txt;
文件:Ada.Text_IO.File_Type;
Line_Count:整数:= 0;
ReturnSize:Integer;
begin
Ada.Text_IO.Open(File => File,
Mode => Ada.Text_IO.In_File,
Name => Filename);
,而Line_Count / = 1循环
申报
行:String:= Ada.Text_IO.Get_Line(File);
begin
ReturnSize:= Integer'Value(Line);
Line_Count:= 1;
end;
结束循环;
Ada.Text_IO.Close(File);
返回ReturnSize;
结束get_size;
begin
null;
末端装备;
我想要做的是将我的 size
整数到由 get_size
返回的值。我怎样才能做到这一点?我曾尝试将我的函数放在spec文件中的 size
变量之前,但它期望文件结束。我尝试设置 size:integer:= gwar.get_size
,但这也不起作用。这可能吗? 最好考虑 manuBriot's 的言论,我想,你仍然可以在技术上遵循Simon Wright的建议。
GENERIC $ b我已经做了一些省略,将重点放在如何为泛型本身的泛型参数赋值。 $ b TYPE项目是私有的; - 数组的类型
size:in out integer; - 数组的大小
包gwar IS
函数get_size返回整数;
END gwar;
与Ada.Text_Io;
使用Ada.Text_Io;
包体gwar是
函数get_size返回整数是
ReturnSize:Integer;
begin
ReturnSize:= Integer'Value(2);
返回ReturnSize;
结束get_size;
begin
大小:= Get_Size;
结束gwar;
这样,当您实例化泛型时,实例主体的效果将是设置参数<$如果 get_size
没有错误地返回,那么将c $ c> size 改为值 2
。
In Ada, I have the following spec file:
GENERIC
TYPE Item IS PRIVATE; --type of array
size : integer; --size of array
PACKAGE gwar IS
function get_size return integer;
END gwar;
And body file:
with Ada.Text_Io;
use Ada.Text_Io;
package body gwar is
--Get_Size allows the txt file to specify how much space to allocate.
function get_size return Integer is
Filename : String := "win.txt";
File : Ada.Text_IO.File_Type;
Line_Count : Integer := 0;
ReturnSize : Integer;
begin
Ada.Text_IO.Open(File => File,
Mode => Ada.Text_IO.In_File,
Name => Filename);
while Line_Count /= 1 loop
declare
Line : String := Ada.Text_IO.Get_Line(File);
begin
ReturnSize := Integer'Value(Line);
Line_Count := 1;
end;
end loop;
Ada.Text_IO.Close (File);
return ReturnSize;
end get_size;
begin
null;
end gear;
What I want to do is set my size
integer to the value returned by get_size
. How can I do this? I have tried putting my function before my size
variable in the spec file, but it expected end of file. I tried setting size : integer := gwar.get_size
, but that does not work either. Is this possible?
Preferably considering manuBriot's remarks, I guess, you could still follow Simon Wright's suggestion technically. I have made a few omission to focus on how to assign a value to a generic parameter in the generic itself.
GENERIC
TYPE Item IS PRIVATE; --type of array
size : in out integer; --size of array
PACKAGE gwar IS
function get_size return integer;
END gwar;
with Ada.Text_Io;
use Ada.Text_Io;
package body gwar is
function get_size return Integer is
ReturnSize : Integer;
begin
ReturnSize := Integer'Value("2");
return ReturnSize;
end get_size;
begin
Size := Get_Size;
end gwar;
This way, when you instantiate the generic, the instance body's effect will be to set the parameter size
to the value 2
, provided that get_size
returns without error.
这篇关于使用函数的返回值在通用包体中进行变量初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!