Ada 记录初始化:"Constraint_Error"
Ada Record Initialization: "Constraint_Error"
我正在尝试在记录中插入新元素。我在记录中设置了默认值并查找了如何初始化记录,但编译器一直说,"warning: null value not allowed here," "warning: 'Constraint_Error' will be raised at run time." 以下是我正在处理的块。
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;
在 begin 之后,我试图设置 tmp 成员的值,这就是我遇到这些错误的地方。
我是否错误地初始化了记录?是否有类似于 Java 中的 try/catch 的东西可以解决这个问题?
我不知道 new 是 Ada 中的东西,但这就是问题所在。与 malloc 类似,为了初始化记录的实例,您使用关键字 new,就像在 Java 中一样。
使用 -gnatl
编译(在包装器内,因此行号相差 3)给出
21. tmp : employeePtr;
所有访问变量默认初始化为null
。我会写
tmp : constant employeePtr := new employee;
(constant
因为你不会改变指针,只是它指向的地方)。
返回代码,
27. tmp.id := eid;
|
>>> warning: null value not allowed here
>>> warning: "Constraint_Error" will be raised at run time
因为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
我认为编译器已停止向重复警告添加详细信息。
30. tmp.jobtitle := title;
31. tmp.payrate := rate;
现在完全停止警告了。
我正在尝试在记录中插入新元素。我在记录中设置了默认值并查找了如何初始化记录,但编译器一直说,"warning: null value not allowed here," "warning: 'Constraint_Error' will be raised at run time." 以下是我正在处理的块。
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;
在 begin 之后,我试图设置 tmp 成员的值,这就是我遇到这些错误的地方。 我是否错误地初始化了记录?是否有类似于 Java 中的 try/catch 的东西可以解决这个问题?
我不知道 new 是 Ada 中的东西,但这就是问题所在。与 malloc 类似,为了初始化记录的实例,您使用关键字 new,就像在 Java 中一样。
使用 -gnatl
编译(在包装器内,因此行号相差 3)给出
21. tmp : employeePtr;
所有访问变量默认初始化为null
。我会写
tmp : constant employeePtr := new employee;
(constant
因为你不会改变指针,只是它指向的地方)。
返回代码,
27. tmp.id := eid;
|
>>> warning: null value not allowed here
>>> warning: "Constraint_Error" will be raised at run time
因为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
我认为编译器已停止向重复警告添加详细信息。
30. tmp.jobtitle := title;
31. tmp.payrate := rate;
现在完全停止警告了。