【问题标题】:How to declare a subtype with a the whole negative range of Integer type in Ada?如何在 Ada 中声明具有整数类型的整个负范围的子类型?
【发布时间】:2016-08-19 13:09:01
【问题描述】:

如果我想声明一个只有正数的 Integer 类型,我需要做的就是:

      subtype Positive is Integer range 0..Integer'Last;

我想我可以应用相同的逻辑来声明这样的否定类型:

      subtype Negative is Integer range Integer'First..1;

然而,在编译时我得到了这个错误:

    attribute "First" may not be used in a subtype mark

为什么会出现此错误,如何声明仅否定的子类型?

【问题讨论】:

  • 您的Negative 子类型包括两个非负值。
  • 不应该Integer'First 给我Integer 类型的最小值吗?
  • 是的,但您的子类型包括值 0 和 1。如果你只想要负值,你需要Integer'First .. -1。

标签: types ada


【解决方案1】:

您的问题可能是由于某处的拼写错误,因为您的确切示例很好:

package SO is
   subtype Negative is Integer range Integer'First..1;
end SO;

编译:

% gnatmake so.ads
gcc-4.9 -c -gnata -gnato -fstack-check -gnat12 -gnatyO -gnatv -gnati1 -gnatf -gnatn so.ads

GNAT 4.9.2
Copyright 1992-2014, Free Software Foundation, Inc.

Compiling: so.ads (source file time stamp: 2016-08-19 05:05:16)
 3 lines: No errors
%

【讨论】:

  • 发现错误,忘记在代码中提及range。谢谢。
  • 爱德华,如果你一开始就发布了你的实际代码,你会更快得到答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多