2016-03-05 75 views
0

我想檢查兩個字符串是否相同,我不斷收到一個錯誤,說操作數是不正確的。 「=」的兩邊都是StateAbbreviation類型,錯誤在哪裏?在你的代碼「無效的操作數類型爲操作數=」ada

with Ada.Text_IO, Ada.Integer_Text_IO; 
use Ada.Text_IO, Ada.Integer_Text_IO; 

procedure ElectionPrediction is 
     MaxCandidates: constant Integer := 100; 
     subtype StateAbbreviation is String (1..2); 
     subtype Initials is String (1..2); 
     type CandidateInfo is record 
       CandidateInitials: Initials; 
       CandidateScore: Integer; 
     end record; 

     type ScoreArray is array (1..MaxCandidates) of CandidateInfo; 

     Score: ScoreArray; 
     CurrentState, HomeState: StateAbbreviation; 
     CandidateName: Initials; 

     function CalculatePointsFromState(CurrentState: StateAbbreviation; CandidateState: StateAbbreviation) return Integer is 
       Total: Integer := 0; 
       temp: Integer := 0; 

     type ScoreArray is array (1..MaxCandidates) of CandidateInfo; 
     type NewEngland is array (1..6) of StateAbbreviation; 
     type NorthEast is array (1..5) of StateAbbreviation; 
     type SouthEast is array (1..12) of StateAbbreviation; 
     type Lakes is array (1..6) of StateAbbreviation; 
     type Central is array (1..8) of StateAbbreviation; 
     type West is array (1..8) of StateAbbreviation; 
     type Pacific is array (1..5) of StateAbbreviation; 

     begin 

       NewEngland := ("ME", "NH", "VT", "MA", "CT", "RI"); 
       NorthEast := ("NY", "PA", "NJ", "DE", "MD"); 
       SouthEast := ("VA", "NC", "SC", "GA", "FL", "AL", "MS", "TN", "KY", "WV", "AR", "LA"); 
       Lakes := ("OH", "MI", "IN", "IL", "WI", "MN"); 
       Central := ("IA", "MO", "ND", "SD", "NE", "KS", "OK", "TX"); 
       West := ("MT", "WY", "CO", "NM", "AZ", "UT", "ID", "NV"); 
       Pacific :=("WA", "OR", "CA", "AK", "HI"); 

       if CandidateState = CurrentState then Total := Total + 50; 
       end if; 
       for I in NewEngland'range loop 
         **if CurrentState = NewEngland(NewEngland'First + I) then temp := temp + 1; end if; 
         if CandidateState = NewEngland(NewEngland'First + I) then temp := temp + 1; end if;** 
       end loop; 
       if temp = 2 then return Total + 20; 
       end if; 

       return 0; 
     end CalculatePointsFromState; 

end ElectionPrediction; 
+1

我認爲你應該先解決你的其他錯誤 – egilhh

+0

一個觀察:'爲我在NewEngland'range循環'...我從'NewEngland'First'開始,所以你想簡化你的數組索引... –

回答

5

此前的錯誤給消息像

39.  NorthEast := ("NY", "PA", "NJ", "DE", "MD"); 
      | 
    >>> invalid use of subtype mark in expression or call 

,因爲你已經定義

29.  type NorthEast is array (1..5) of StateAbbreviation; 

NorthEast在(子)類型,而不是一個變量!而這個嚴重的錯誤讓編譯器感到困惑,後面的錯誤信息並沒有儘可能地讓人感覺到。

你可能會考慮針對的StateAbbreviation小號任意長度的數組

type StatesArray is array (Positive range <>) of StateAbbreviation; 

創建類型,然後創建區域數據作爲特定的(什麼常數:你不希望你的程序通過覆蓋它們錯誤)這種類型的陣列

NewEngland : constant StatesArray := ("ME", "NH", "VT", "MA", "CT", "RI」); 
NorthEast : constant StatesArray := ("NY", "PA", "NJ", "DE", "MD"); 
... 

之後其餘的代碼將編譯OK。

+0

謝謝爲了您的幫助,它修復了一切。我以前並不理解它,但你已經清除了它,+1 – Frank

相關問題