将类型转换为字符串
Convert type to string
我创建了一个类型:
packages MyTypes.Type_A is
subtype Counter_Type is Integer range 0 .. 15;
type Array_Counter_Type is record
Counter_01 : Counter_Type ;
Counter_02 : Counter_Type ;
Counter_03 : Counter_Type ;
Counter_04 : Counter_Type ;
end record;
end MyTypes.Type_A;
我想这样显示我的数组
MyArray : Array_Counter_Type;
print ( MyTypes.Type_A.Array_Counter_Type'Image (MyArray));
但是我有错误:
prefix og "Image" attribute must be scalar type
我该怎么办?是否可以 "customize" Image
连接用 '-' 分割的 4 个计数器?
这还不可能,但它将在 Ada 202x [AI12-0020-1]. Until then you will have to define a subprogram (e.g Image
) and call it explicitly. See also this related question SO 上出现。
示例使用 GNAT.Formatted_String
:
main.adb
with Ada.Text_IO;
with GNAT.Formatted_String;
procedure Main is
subtype Counter_Type is Integer range 0 .. 15;
type Array_Counter_Type is
record
Counter_01 : Counter_Type;
Counter_02 : Counter_Type;
Counter_03 : Counter_Type;
Counter_04 : Counter_Type;
end record;
-----------
-- Image --
-----------
function Image (Array_Counter : Array_Counter_Type) return String is
use GNAT.Formatted_String;
Format : constant Formatted_String := +"%02d-%02d-%02d-%02d";
begin
return
-(Format
& Array_Counter.Counter_01
& Array_Counter.Counter_02
& Array_Counter.Counter_03
& Array_Counter.Counter_04);
end Image;
AC : Array_Counter_Type := (0, 5, 10, 15);
begin
Ada.Text_IO.Put_Line (Image (AC));
end Main;
但如果 GNAT.Formatted_String
不可用,您也可以使用 Ada.Integer_Text_IO
、Counter_Type'Image
等。
我创建了一个类型:
packages MyTypes.Type_A is
subtype Counter_Type is Integer range 0 .. 15;
type Array_Counter_Type is record
Counter_01 : Counter_Type ;
Counter_02 : Counter_Type ;
Counter_03 : Counter_Type ;
Counter_04 : Counter_Type ;
end record;
end MyTypes.Type_A;
我想这样显示我的数组
MyArray : Array_Counter_Type;
print ( MyTypes.Type_A.Array_Counter_Type'Image (MyArray));
但是我有错误:
prefix og "Image" attribute must be scalar type
我该怎么办?是否可以 "customize" Image
连接用 '-' 分割的 4 个计数器?
这还不可能,但它将在 Ada 202x [AI12-0020-1]. Until then you will have to define a subprogram (e.g Image
) and call it explicitly. See also this related question SO 上出现。
示例使用 GNAT.Formatted_String
:
main.adb
with Ada.Text_IO;
with GNAT.Formatted_String;
procedure Main is
subtype Counter_Type is Integer range 0 .. 15;
type Array_Counter_Type is
record
Counter_01 : Counter_Type;
Counter_02 : Counter_Type;
Counter_03 : Counter_Type;
Counter_04 : Counter_Type;
end record;
-----------
-- Image --
-----------
function Image (Array_Counter : Array_Counter_Type) return String is
use GNAT.Formatted_String;
Format : constant Formatted_String := +"%02d-%02d-%02d-%02d";
begin
return
-(Format
& Array_Counter.Counter_01
& Array_Counter.Counter_02
& Array_Counter.Counter_03
& Array_Counter.Counter_04);
end Image;
AC : Array_Counter_Type := (0, 5, 10, 15);
begin
Ada.Text_IO.Put_Line (Image (AC));
end Main;
但如果 GNAT.Formatted_String
不可用,您也可以使用 Ada.Integer_Text_IO
、Counter_Type'Image
等。