2016-03-05 67 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