[英]Ada Record Initialization: “Constraint_Error”
I'm trying to insert new elements in a record. 我正在尝试在记录中插入新元素。 I have default values set in the record and looked up how to initialize a record, but the compiler keeps saying, "warning: null value not allowed here," "warning: 'Constraint_Error' will be raised at run time."
我在记录中设置了默认值,并查找了如何初始化记录,但编译器一直说,“警告:此处不允许使用null值”,“警告:'Constraint_Error'将在运行时引发。” The following is the block I am working on.
以下是我正在处理的块。
type employee;
type employeePtr is access employee;
type employee is
record
id : Integer := 0;
name : Unbounded_String := To_Unbounded_String("");
departmentname : Unbounded_String := To_Unbounded_String("");
jobtitle : Unbounded_String := To_Unbounded_String("");
payrate : Unbounded_String := To_Unbounded_String("");
next : employeePtr := null;
end record;
employeeList : employeePtr;
procedure insertNew(employeeid : Unbounded_String; employeename : Unbounded_String; department : Unbounded_String; title : Unbounded_String; rate : Unbounded_String) is
currentEmployee : employeePtr;
--tmp : employeePtr := employeePtr'(id => 0, name => "", departmentname => "", jobtitle => "", payrate => "");
tmp : employeePtr;
tmpSkp : employeePtr;
eid : Integer;
placeAtEnd : Integer;
begin
eid := Integer'Value(To_String(employeeid));
tmp.id := eid;
tmp.name := employeename;
tmp.departmentname := department;
tmp.jobtitle := title;
tmp.payrate := rate;
if employeeList.id = 0 then
employeeList := tmp;
else
placeAtEnd := 1;
while currentEmployee.next /= null loop
if currentEmployee.next.id > eid then
tmpSkp := currentEmployee.next;
tmp.next := tmpSkp;
currentEmployee.next := tmp;
placeAtEnd := 0;
exit;
else
currentEmployee := currentEmployee.next;
end if;
end loop;
if placeAtEnd = 1 then
currentEmployee.next := tmp;
end if;
end if;
end insertNew;
Right after begin , where I am trying to set the values of members of tmp, is where I am getting these errors. 在开始之后,我试图设置tmp成员的值,这是我得到这些错误的地方。 Am I initializing the record incorrectly?
我是否错误地初始化了记录? Is there something similar to try/catch from Java that could fix this?
是否有类似于Java的try / catch可以解决这个问题?
Compiling with -gnatl
(inside a wrapper, so the line numbers are off by 3) gives 使用
-gnatl
编译(在包装器内部,因此行号关闭3)给出
21. tmp : employeePtr;
All access variables are default-initialized to null
. 所有访问变量都默认初始化为
null
。 I'd have written 我写过
tmp : constant employeePtr := new employee;
( constant
because you're not going to change the pointer, just what it points to). (
constant
因为你不会改变指针,只是指向它)。
Returning to the code, 回到代码,
27. tmp.id := eid;
|
>>> warning: null value not allowed here
>>> warning: "Constraint_Error" will be raised at run time
because Constraint_Error
is the required run time error for accessing via a null pointer. 因为
Constraint_Error
是通过空指针访问所需的运行时错误。
28. tmp.name := employeename;
|
>>> warning: null value not allowed here
>>> warning: "Constraint_Error" will be raised at run time
29. tmp.departmentname := department;
|
>>> warning: "tmp" may be null
I think the compiler has stopped adding detail to the repeated warning. 我认为编译器已停止向重复警告添加细节。
30. tmp.jobtitle := title;
31. tmp.payrate := rate;
and now it's stopped warning at all. 现在它已经停止了警告。
I didn't know that new is something in Ada, but that was the problem. 我不知道Ada中的新东西是什么,但那就是问题所在。 Similar to malloc, in order to initialize an instance of a record, you use the keyword new , just like in Java.
与malloc类似,为了初始化记录的实例,您可以使用关键字new ,就像在Java中一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.