为什么我得到“类型没有typeinfo”枚举类型的错误

编程入门 行业动态 更新时间:2024-10-08 18:34:43
本文介绍了为什么我得到“类型没有typeinfo”枚举类型的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经宣布了以下枚举类型,其中我希望第一个成员的序数值为1(一),而不是通常的0(零):

键入 TMyEnum =( meFirstValue = 1, meSecondValue, meThirdValue );

如果我打电话给 TypeInfo()作为调用 GetEnumName()的一部分,我收到编译器错误:

GetEnumName(TypeInfo (TMyEnum),Ord(aValue));

错误:E2134:键入'TMyEnum'没有typeinfo / p>

为什么是这样?

我知道如果使用 $ M 编译器选项启用或(派生自某些类,例如 TPersistent ),但我并没有想到有类型信息用于枚举类型的特殊条件。

解决方案

类型信息不支持枚举特定的序数值,导致枚举成员具有与通常不同的序数值的枚举值由编译器分配。

如果具体值是必需或不可取的,那么必须插入未使用枚举成员才能根据需要填写枚举。例如(仅用于强调的附加缩进):

键入 TMyEnum =( meNOTUSED1,{= 0 } meFirstValue,{= 1} meSecondValue, meThirdValue );

然后可以使用子范围来过滤未使用的初始值:

TValidMyEnum = meFirstValue..meThirdValue;

尽管您可能希望考虑重命名原始枚举类型,以便您的子范围类型可以全部使用您的项目。

如果枚举包含差距,则子范围是不够的:

键入 TMyEnum =( meNOTUSED1,{= 0} meFirstValue,{= 1} meSecondValue, meThirdValue, meNOTUSED2, meFinalValue {= 5} );

在这种情况下,没有简单的方法来扩展编译时间范围检查以排除未使用的成员,但是几种类型的集合将简化实施任何必要的运行时检查的业务:

type TMyEnums =一组TMyEnum; const meNOTUSED = [meUNUSED1,meUNUSED2]; // .. etc as required meValidValues = [Low(TMyEnum).. High(TMyEnum)] - meNOTUSED; if NOT(aValue in meValidValues)then // etc

I have declared the following enum type in which I want the first member to have the ordinal value of 1 (one) rather than the usual 0 (zero):

type TMyEnum = ( meFirstValue = 1, meSecondValue, meThirdValue );

If I call TypeInfo(), e.g. as part of a call to GetEnumName(), I get a compiler error:

GetEnumName(TypeInfo(TMyEnum), Ord(aValue));

ERROR: "E2134: Type 'TMyEnum' has no typeinfo"

Why is this?

I know that classes only have typeinfo if they are compiled with the $M compiler option enabled or (derive from some class which was, such as TPersistent) but I didn't think there were any special conditions for having typeinfo for enum types.

解决方案

Type information is not supported for enums where specific ordinal values are assigned that result in enum members having ordinal values that are different to those that would normally be assigned by the compiler.

If specific values are essential or desirable, "unused" enum members will have to be inserted to "pad" the enum as required. e.g (additional indentation for emphasis only):

type TMyEnum = ( meNOTUSED1, {= 0} meFirstValue, {= 1} meSecondValue, meThirdValue );

A subrange can then be used to "filter" out the unused initial value:

TValidMyEnum = meFirstValue..meThirdValue;

Although you might then wish to consider renaming the original enum type so that your subrange type may be used throughout your project.

A subrange isn't sufficient if the enum contains "gaps":

type TMyEnum = ( meNOTUSED1, {= 0} meFirstValue, {= 1} meSecondValue, meThirdValue, meNOTUSED2, meFinalValue {= 5} );

In this case there is no simply way to extend compile-time range checking to exclude the unused members, but a couple of set types will simplify the business of implementing any necessary runtime checks:

type TMyEnums = set of TMyEnum; const meNOTUSED = [meUNUSED1, meUNUSED2]; // .. etc as required meValidValues = [Low(TMyEnum)..High(TMyEnum)] - meNOTUSED; if NOT (aValue in meValidValues) then // etc

更多推荐

为什么我得到“类型没有typeinfo”枚举类型的错误

本文发布于:2023-11-15 19:46:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1597423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   错误   typeinfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!