簡體   English   中英

Ada記錄初始化:“Constraint_Error”

[英]Ada Record Initialization: “Constraint_Error”

我正在嘗試在記錄中插入新元素。 我在記錄中設置了默認值,並查找了如何初始化記錄,但編譯器一直說,“警告:此處不允許使用null值”,“警告:'Constraint_Error'將在運行時引發。” 以下是我正在處理的塊。

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;

開始之后,我試圖設置tmp成員的值,這是我得到這些錯誤的地方。 我是否錯誤地初始化了記錄? 是否有類似於Java的try / catch可以解決這個問題?

使用-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;

現在它已經停止了警告。

我不知道Ada中的東西是什么,但那就是問題所在。 與malloc類似,為了初始化記錄的實例,您可以使用關鍵字new ,就像在Java中一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM