Cha*_*haa 45 delphi delphi-xe2
我开始使用Delphi 2010项目,然后迁移到XE,现在我尝试迁移到XE2.在XE2(Update 4 Hotfix 1)中进行编译后,单元测试开始失败.经过一些调试后,很明显以下代码未正确编译:
program ForwardDeclaration;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TEntityBase = class(TObject)
protected
FModel: Integer;
public
constructor Create(const AModel: Integer);
end;
TEntity<TKey> = class(TEntityBase)
end;
TMyEntity2 = class;
TMyEntity1 = class(TEntity<Integer>)
FData: Integer;
end;
TMyEntity2 = class(TMyEntity1)
end;
constructor TEntityBase.Create(const AModel: Integer);
begin
inherited Create;
FModel := AModel;
end;
var
MyEntity: TMyEntity1;
begin
try
Writeln(TEntityBase.ClassName, ': ', TEntityBase.InstanceSize, ' bytes');
Writeln(TMyEntity1.ClassName, ': ', TMyEntity1.InstanceSize, ' bytes');
MyEntity := TMyEntity1.Create(100);
Assert(MyEntity.FData = 0);
except
on E: Exception do Writeln(E.ClassName, ': ', E.Message);
end;
end.
Run Code Online (Sandbox Code Playgroud)
方案产出:
TEntityBase: 12 bytes
TMyEntity1: 12 bytes <-- Must be 16 bytes!
EAssertionFailed: Assertion failure (ForwardDeclaration.dpr, line 41)
Run Code Online (Sandbox Code Playgroud)
是否可以通过调整编译器选项来解决问题?
这个问题是否会在其他人身上重复出现?
PS QC107110
是否可以通过调整编译器选项来解决问题?
不,您无法通过调整来修复错误,这是编译器中的一个(非常具体的)错误。
[谁能告诉我]这个问题是否在其他人身上重复出现?
我可以重现代码,但只能在 XE2 update 4 中。
我无法在 XE3 中检查它(没有该版本)。它在 XE4 中已修复(根据评论)。
因此,让代码正常工作的唯一方法是:
A。删除不需要的前向声明。
b. 使用不同版本的 Delphi。