【问题标题】:"Size of published set '%s' is >4 bytes". How to fix this compiler error?“已发布集 '%s' 的大小 > 4 字节”。如何修复此编译器错误?
【发布时间】:2010-01-23 16:54:13
【问题描述】:

我有一组包含 138 个值的枚举值。比如:

type
  TSomething = (sOne, sTwo, sThree, ..., ..., sOnehundredAndThirtyeight);
  TSomethings = set of TSomething;

....

  TSomething = class(TPersistent)
  private
    fSomethings: TSomethings;
  published
    property Somethings: TSomethings read fSomethings write fSomethings;
  end;

编译时我收到以下错误消息:

[DCC Error] uProfilesManagement.pas(20): E2187 Size of published set 'Something' is >4 bytes

知道如何在已发布的属性中包含一组这种大小吗?

我需要在已发布部分包含此集,因为我使用 OmniXMLPersistent 将类保存到 XML 中,它只保存已发布的属性。

【问题讨论】:

  • 错误信息中真的是 %s 吗?如果是,您应该将其作为错误报告给质量中心,因为他们显然忘记调用 Format 或没有通过此错误消息传递属性名称。
  • 不,错误消息是“[DCC Error] uProfilesManagement.pas(20): E2187 Size of published set 'Something' is >4 bytes”,如问题文本所示。

标签: delphi


【解决方案1】:

可能你需要以下技巧(我没有使用 OmniXML,无法检查):

type
  TSomething = (sOne, sTwo, sThree, ..., sOnehundredAndThirtyeight);
  TSomethings = set of TSomething;

  TSomethingClass = class(TPersistent)
  private
    fSomethings: TSomethings;
    function GetSomethings: string;
    procedure SetSomethings(const Value: string);
  published
    property Somethings: string read GetSomethings write SetSomethings;
  end;


{ TSomethingClass }

function TSomethingClass.GetSomethings: string;
var
  thing: TSomeThing;
begin
  Result:= '';
  for thing:= Low(TSomething) to High(TSomething) do begin
    if thing in fSomethings then Result:= Result+'1'
    else Result:= Result+'0';
  end;
end;

procedure TSomethingClass.SetSomethings(const Value: string);
var
  I: Integer;
  thing: TSomeThing;
begin
  fSomethings:= [];
  for I:= 0 to length(Value) - 1 do begin
    if Value[I+1] = '1' then Include(fSomethings, TSomething(I));
  end;
end;

【讨论】:

  • 感谢您的回复。关于如何使用 GetSetProp 和 SetSetProp(来自 TypInfo 单元)自动将集合作为字符串并将其从字符串设置回原始状态的任何想法?我刚刚为此专门创建了另一个问题:stackoverflow.com/questions/2127079/…
【解决方案2】:

不幸的是,编译器不允许在已发布的节中包含大于 32 位的集合。集合的大小(以字节为单位)可以通过High(set) div 8 - Low(set) div 8 + 1 计算。

您可以减小集合大小,或者使用自定义类代替集合,或者您可以将枚举拆分为几个集合,每个集合的大小为 32 位。

【讨论】:

  • 感谢您的回复。拆分枚举将涉及对应用程序代码的大量更改,因此这是不可能的。知道如何创建一个可以复制该集合的自定义类吗?
  • @smartin :不幸的是,您需要 18 个字节。也许将它类型转换为足够大的字节数组?
猜你喜欢
  • 1970-01-01
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
相关资源
最近更新 更多