为字符串解析创建自定义枚举器时,我看到一些奇怪的错误消息。使用记录时出现以下错误:
E2010不兼容的类型:'TSplitStringEnumerator'和'指针'
当使用类(在代码中插入一些.Create调用)而不是记录时,我会不时得到一些内部错误:
有谁知道如何使枚举与记录数据类型而不是类一起工作?
type
TSplitStringEnumerator = record
StringToParse: string;
Separator: Char;
S: Integer;
E: Integer;
L: Integer;
function GetCurrent (): string; inline;
function MoveNext (): Boolean; inline;
property Current: string read GetCurrent;
end;
TSplitStringGenerator = record
Enum: TSplitStringEnumerator;
function GetEnumerator: TSplitStringEnumerator; inline;
end;
function SplitString( const StringToParse: string; Separator: Char ): TSplitStringGenerator; //inline;
begin
Result.Enum.StringToParse := StringToParse;
Result.Enum.Separator := Separator;
Result.Enum.S := 0;
Result.Enum.E := 0;
Result.Enum.L := Length( StringToParse );
end;
procedure Test();
var
S: string;
begin
for S in SplitString( 'A;B;C', ';' ) do begin
OutputDebugString( PChar( S ) );
end;
end;
{ TSplitStringGenerator }
function TSplitStringGenerator.GetEnumerator(): TSplitStringEnumerator;
begin
Result := Enum;
end;
{ TSplitStringEnumerator }
function TSplitStringEnumerator.GetCurrent(): string;
begin
Result := Copy( StringToParse, S, E - S );
end;
function TSplitStringEnumerator.MoveNext(): Boolean;
begin
S := E + 1;
Result := S <= L;
E := S;
while ( E <= L ) and ( StringToParse[ E ] <> Separator ) do Inc( E );
end;
答案 0 :(得分:4)
我在QC上发现了类似的bug report #72213。该错误已在Delphi 2010中修复(请参阅解决方案评论)。
答案 1 :(得分:2)
该代码编译并似乎在Delphi 2010中成功运行。输出为:
Debug Output: A Process Project4.exe (4656)
Debug Output: B Process Project4.exe (4656)
Debug Output: C Process Project4.exe (4656)
是否有可能在Delphi 2007中不支持具有记录的枚举器?