加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 运算符不适用于此操作数类型

发布时间:2020-12-15 09:22:47 所属栏目:大数据 来源:网络整理
导读:我有这种情况: type TWheel = record private type TWheelEnum = (whBA,whCA,whFI,whGE,whMI,whNA,whPA,whRM,whRN,whTO,whVE); var FWheelEnum: TWheelEnum; public class operator Implicit(const Value: string): TWheel; class operator Implicit(const
我有这种情况:

type
  TWheel = record
  private type
    TWheelEnum = (whBA,whCA,whFI,whGE,whMI,whNA,whPA,whRM,whRN,whTO,whVE);
  var
    FWheelEnum: TWheelEnum;
  public
    class operator Implicit(const Value: string): TWheel;
    class operator Implicit(const Value: TWheel): string;
  end;

有:

var
 awheel,bwheel: twheel;
begin
  try
    awheel := 'PA';
    bwheel := 'PA';
    if awheel = bwheel then writeln ('pippo');
end.

当我运行它时转向我这个错误:

E2015 Operator not applicable to this operand type

我已经解决了写作:

if awheel = string(bwheel) then writeln ('pippo');

但是可以在没有添加字符串(…)的情况下解决它吗?在第一时刻,我认为是:

class operator Implicit(const Value: TWheel): Twheel;

但编译器给我错误,告诉我只接受一个TWheel类型.所以我想知道是否有解决方案,或者我是否需要使用带字符串(…)的转换类型?
非常感谢.

解决方法

您需要定义一个Equal运算符:

class operator Equal(const a,b: TWheel): Boolean;

我想实现应该是:

class operator TWheel.Equal(const a,b: TWheel): Boolean;
begin
  Result := a.FWheelEnum=b.FWheelEnum;
end;

您可能还想要实现NotEqual运算符.

class operator TWheel.NotEqual(const a,b: TWheel): Boolean;
begin
  Result := a.FWheelEnum<>b.FWheelEnum;//could write not (a=b) instead
end;

事实上,这些运算符的定义足以让编译器接受与混合TWheel和字符串操作数的相等比较.

完整的运算符列表在documentation中提供,偶尔阅读一下,以便重新了解运算符重载的可用内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读