阿达 : put() with custom type

标签 ada

我正在寻找一种将 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,并且没有采用 Count 类型参数的子程序 Put。一些选项:

  • 选项 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;

关于阿达 : put() with custom type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249229/

相关文章:

linux - 降级 gcc 版本后,Ada 编译器崩溃并显示 "Ada compiler not installed on this system."

c++ - C++ 中的枚举类似于 Ada 中的枚举?

STM32F4 (Cortex-M4) 上的 Ada

syntax - Ada 中的自定义“图像”属性?

assembly - Ada 和组装

types - Ada 将有限的私有(private)类型传递给任务

uuid - 如何在 Ada 中生成唯一 ID?

ada - Ada 中 protected 对象内部的访问类型

ada - 成员值的静态引用 - Ada

ada - 是否可以声明具有无限上限的 Ada 范围?