我在Ada运行程序时遇到了一些麻烦。我有以下三个项目文件(我使用GPS):
Types.ads
包类型是 子类型T_valeurind是整数范围2..14; 输入T_couleur …
首先,那些不是项目文件,它们具有类型 .gpr ;它们是项目中的Ada源文件。
.gpr
您的 types.ads 许诺 function trans ,这意味着它需要一个包体 types.adb ,
types.ads
function trans
types.adb
package body types is function trans(val : Character) return T_valeurind is ret: Integer; begin case val is when '3' => ret:=3; when '4' => ret:=4; when '5' => ret:=5; when '6' => ret:=6; when '7' => ret:=7; when '8' => ret:=8; when '9' => ret:=9; when 'T' => ret:=10; when 'J' => ret:=11; when 'Q' => ret:=12; when 'K' => ret:=13; when 'A' => ret:=14; when others => null; end case; return ret; end trans; end types;
(嗯。如果你传入一个无效的字符,你会返回未初始化的数据,就像没有得到一个 Constraint_Error ; T_valeurind 包括值2,你不应该覆盖吗?)
Constraint_Error
T_valeurind
您的 trans.adb 而是指定库级函数。
trans.adb
当我在GPS中构建文件时,一切正常。但是当我想运行它们时,我有以下消息,并且没有执行:
如果包装规格( types.ads )需要一个身体( types.adb 并且您没有提供它,编译器将在您尝试编译时生成您报告的消息。如果你试着 编 test.adb 一切都会安好的。如果你试着 建立 test.adb 它会尝试编译包 Types 并且无论你是否正在尝试构建或构建&跑。
test.adb
Types
我不知道这是如何第一次工作的!
看起来你忘了包括你的 function Trans 在测试程序的上下文中。如果它不在上下文中,则不能使用它。
function Trans
尝试添加:
with Trans;
到上下文的子句 procedure Test 。
procedure Test
使用强大的Ada枚举功能(以及一些糟糕的输入处理策略,例如异常),首先可以避免您的整个问题。您的 trans 程序没用。
trans
如果您对枚举值的顺序关系感兴趣,也可以使用Ada 的 'First * 强> (第一个枚举文字), 的 'Last 强> (最后一个文字), 的 'Pos 强> (枚举内的位置), 的 'Succ 强> (下一个enum文字), 的 'Pred 强> (以前的枚举文字)。
'First
'Last
'Pos
'Succ
'Pred
如果为变量执行内存映射,则可以使用 的 'Valid 强> 检查变量是否具有有效值,并为约束错误保存异常捕获的需要。
'Valid
见下面的例子:
with Ada.Text_IO; use Ada.Text_IO; with Ada.Exceptions; use Ada.Exceptions; procedure Hello is -- miwing chars and literal values in enum -- note that jack is 'J' and not the single source code character J type My_Awesome_Enum is ('1', '2', '3', 'J', Q, K, Ace); for My_Awesome_Enum use ('1' => -1, '2' => 2, '3' => 3, -- ... 'J' => 11, Q => 12, K => 13, Ace => 14); temp : Integer; prev : My_Awesome_Enum; succ : My_Awesome_Enum; temp2 : My_Awesome_Enum; begin -- ------------------------------------------ -- Ada enum power declare begin for value in My_Awesome_Enum loop temp := My_Awesome_Enum'Enum_Rep(value); Put_Line("Enum litteral value: " & value'Image & " - memory representation: " & Integer'Image(temp)); if value /= My_Awesome_Enum'First then prev := My_Awesome_Enum'Pred(value); Put_Line("Previous: " & prev'Image); else Put_Line("No previous"); end if; if value /= My_Awesome_Enum'Last then succ := My_Awesome_Enum'Succ(value); Put_Line("Next: " & succ'Image); else Put_Line("No next"); end if; Put_Line(""); end loop; end; -- ------------------------------------------ -- conversion from some input source Put_Line(""); declare strInput : String := "Unknown user value"; begin Put_Line("Handling of user input: " & strInput); temp2 := My_Awesome_Enum'Value (strInput); exception when E: others => Put_Line("Exception catched: " & Exception_Information (E)); Put_Line("Setting value to Ace instead"); temp2 := Ace; end; Put_Line("tmp2 value: " & temp2'Image & " - memory representation: " & Integer'Image(My_Awesome_Enum'Enum_Rep(temp2))); -- ------------------------------------------ -- mmemory mapping Put_Line(""); declare my_int : Integer := -3; mapped_Enum : My_Awesome_Enum; for mapped_Enum'Address use my_int'Address; last_enum : My_Awesome_Enum := (My_Awesome_Enum'Last); stop_condition : Integer := (last_enum'Enum_Rep) + 2; begin while (my_int < stop_condition) loop if mapped_Enum'Valid then Put_Line("Enum with value: " & my_int'Image & " is valid."); else Put_Line("Memory mapping would result in invalid enum for value: " & my_int'Image); end if; my_int := my_int + 1; end loop; end; end Hello;
这给出了以下输出( https://www.tutorialspoint.com/compile_ada_online.php ,使用GNATMAKE v7.1.1):
建立:
$gnatmake -o hello *.adb gcc -c hello.adb gnatbind -x hello.ali gnatlink hello.ali -o hello
执行:
Enum litteral value: '1' - memory representation: -1 No previous Next: '2' Enum litteral value: '2' - memory representation: 2 Previous: '1' Next: '3' Enum litteral value: '3' - memory representation: 3 Previous: '2' Next: 'J' Enum litteral value: 'J' - memory representation: 11 Previous: '3' Next: Q Enum litteral value: Q - memory representation: 12 Previous: J Next: K Enum litteral value: K - memory representation: 13 Previous: Q Next: ACE Enum litteral value: ACE - memory representation: 14 Previous: K No next Handling of user input: Unknown user value Exception catched: raised CONSTRAINT_ERROR : bad input for 'Value: "Unknown user value" Setting value to Ace instead tmp2 value: ACE - memory representation: 14 Memory mapping would result in invalid enum for value: -3 Memory mapping would result in invalid enum for value: -2 Enum with value: -1 is valid. Memory mapping would result in invalid enum for value: 0 Memory mapping would result in invalid enum for value: 1 Enum with value: 2 is valid. Enum with value: 3 is valid. Memory mapping would result in invalid enum for value: 4 Memory mapping would result in invalid enum for value: 5 Memory mapping would result in invalid enum for value: 6 Memory mapping would result in invalid enum for value: 7 Memory mapping would result in invalid enum for value: 8 Memory mapping would result in invalid enum for value: 9 Memory mapping would result in invalid enum for value: 10 Enum with value: 11 is valid. Enum with value: 12 is valid. Enum with value: 13 is valid. Enum with value: 14 is valid. Memory mapping would result in invalid enum for value: 15