首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“operand = "ada的操作数类型无效

“operand = "ada的操作数类型无效
EN

Stack Overflow用户
提问于 2016-03-05 11:54:08
回答 1查看 377关注 0票数 0

我正在尝试检查两个字符串是否相同,但我一直收到一个错误,说操作数不正确。"=“的两边都是StateAbbreviation类型,错误在哪里?

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2016-03-05 17:26:15

代码中的早期错误会给出如下消息

代码语言:javascript
复制
39.       NorthEast := ("NY", "PA", "NJ", "DE", "MD");
          |
    >>> invalid use of subtype mark in expression or call

因为您已经定义了

代码语言:javascript
复制
29.       type NorthEast is array (1..5) of StateAbbreviation;

NorthEast是(子)类型,而不是变量!这个严重的错误使编译器感到困惑,以至于后来的错误消息没有足够的意义。

您可以考虑为任意长度的StateAbbreviation数组创建一个类型。

代码语言:javascript
复制
type StatesArray is array (Positive range <>) of StateAbbreviation;

然后将区域数据创建为这种类型的特定(常量:您不希望程序错误地覆盖它们)数组

代码语言:javascript
复制
NewEngland : constant StatesArray := ("ME", "NH", "VT", "MA", "CT", "RI”);
NorthEast  : constant StatesArray := ("NY", "PA", "NJ", "DE", "MD");
...

在此之后,其余代码将正常编译。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35809787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档