ada - Ada : 自定义类型的 put()
问题描述
我正在寻找一种将 Put() 函数与我创建的自定义类型一起使用的方法。我怎么能这样做?
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure main is
type count is range -2..2 ;
begin
Put(counter);
end main;
这就是我得到的:
Builder results
C:\Users\***********\Desktop\ada project\src\main.adb
26:11 expected type "Standard.Integer"
26:7 no candidate interpretations match the actuals:
26:7 possible missing instantiation of Text_IO.Integer_IO
解决方案
您缺少实例 ,Counter
并且没有Put
接受类型参数的子程序Count
。一些选项:
- 选项 1 - 使用该
Image
属性。
with Ada.Text_IO;
procedure Main is
type Count is range -2 .. 2;
Counter : Count := 1;
begin
Ada.Text_IO.Put (Counter'Image); -- Counter'Image returns a String
-- or
Ada.Text_IO.Put (Count'Image (Counter));
end Main;
- 选项 2 - 强制类型转换
Integer
。
with Ada.Integer_Text_IO;
procedure Main is
type Count is range -2 .. 2;
Counter : Count := 1;
begin
Ada.Integer_Text_IO.Put (Integer (Counter));
end Main;
- 选项 3 - 定义子类型而不是类型。
with Ada.Integer_Text_IO;
procedure Main is
subtype Count is Integer range -2 .. 2;
Counter : Count := 1;
begin
Ada.Integer_Text_IO.Put (Counter);
end Main;
- 选项 4 - 实例化通用包
Ada.Text_IO.Integer_IO
。
with Ada.Text_IO;
procedure Main is
type Count is range -2 .. 2;
Counter : Count := 1;
package Count_Text_IO is
new Ada.Text_IO.Integer_IO (Count);
begin
Count_Text_IO.Put (Counter);
end Main;
推荐阅读
- html - 在Angular 5 App中首次加载App时背景视频不播放
- python - 使用排序的 QAbstractTableModel 时如何将“选择项目...”添加到 QComboBox?
- r - 网络抓取足球数据什么也不返回
- r - R中分组数据的条件格式
- javascript - JSLint “这个函数需要一个‘use strict’ pragma。”
- html - 过滤 url 之间的所有内容
- google-cloud-platform - Google Cloud Datastore:使用 Node.js 批量导入
- android - 使用 .so 和 .com 文件打包到 .aar 库中
- powershell - 如何延迟 IIS 重启服务器?
- mongodb - Mongo:创建唯一的文本索引