记录录入(无法初始化) [英] Record in record (Cannot initialize)
问题描述
我有2条记录如下:
TYPE
TRecord2= packed record
i2: Integer;
end;
TRecord1= packed record
i1: Integer;
R2: TRecord2;
end;
。
我想初始化将字段记录到零,但是我不想使用FillMemory,所以我声明了2个常量记录,其中我初始化了这些字段。
I want to initialize the record fields to zero but I don't want to use FillMemory so I declared 2 constant records in which I initialize the fields.
CONST
Record2c: TRecord2=
(
i2: 0;
);
Record1c: TRecord1=
(
i1: 0;
R2: Record2c; <------- error line
);
但是,我不能将Record2c分配给R2字段。编译器说:E2029'('expected but identifier'Record2c'found。
However, I cannot assign a Record2c to R2 field. The compiler says: E2029 '(' expected but identifier 'Record2c' found.
但是这样做(如果我在评论行错误的地方):
But this works (if I comment the line where I have the error):
procedure test;
var Record1: TRecord1;
begin
Record1:= Record1c; // initialize variable by associating the constant to it
end
那么,我如何初始化R2字段
So, how do i initialize the R2 field?
推荐答案
只能使用真实常量初始化const ,真实的常量没有类型 - 是键入的常量。请参阅 已声明的常量 一个>在Delphi文档中。你的代码中的 Record2c
是一个类型的常量,所以它不能在const表达式中使用,像初始化 Record1c
。你只需要在线定义 Record1c.R2
:
You can only initialize consts with true constants. True constants do not have types — those are typed constants. See Declared Constants in the Delphi documentation. Record2c
in your code is a typed constant, so it cannot be used in const expressions like the one required for initializing Record1c
. You'll just have to in-line the definition of Record1c.R2
:
const
Record1c: TRecord1 = (
i1: 0;
R2: (i2: 0;);
);
当您注释掉错误行时,您将离开 R2
字段默认初始化为零。
When you comment out the error line, you're leaving the R2
field default-initialized to zeros.
这篇关于记录录入(无法初始化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!