如何解决pascal 中记录的横向隐式/显式类型对话
我想在pascal中实现两种记录类型的横向类型对话。但是我收到错误“错误:过程或函数预期”。这是相关代码:
{$mode objfpc}{$H+}
interface
{$modeswitch advancedrecords}
uses
Classes,SysUtils;
type
TRGBA8 = packed record
r,g,b,a: byte;
public
constructor Create(r_in,g_in,b_in,a_in: byte);
end;
TRGBA16 = packed record
r,a: word;
public
constructor Create(r_in,a_in: word);
class operator Explicit(const c_in: TRGBA16): TRGBA8; inline;
class operator :=(const c_in: TRGBA16): TRGBA8; inline;
end;
TRGBA8Helper = record helper for TRGBA8
public
class operator Explicit(const c_in: TRGBA8): TRGBA16;
end;
我在 TRGBA8Helper 的 Operator Explicit 声明中收到错误。如何存档横向类型的对话。提前致谢。
我正在使用来自 Ubuntu 存储库的 Lazarus 1.8.2 和 FPC 3.0.4。
解决方法
我找到了解决方案。我的第一次尝试是从 FPC 源 (/usr/share/fpcsrc/3.0.4/rtl/objpas/types.pp) 的 TPoint 类派生的。在 wiki.freepascal.org/Operator_overloading 中可以找到指向文档的链接:https://www.freepascal.org/docs-html/ref/refch15.html。此页面包含示例。所以我来到了这个解决方案:
{$mode objfpc}{$H+}
interface
{$modeswitch advancedrecords}
uses
Classes,SysUtils;
type
TRGBA8 = packed record
r,g,b,a: byte;
public
constructor Create(r_in,g_in,b_in,a_in: byte);
end;
TRGBA16 = packed record
r,a: word;
public
constructor Create(r_in,a_in: word);
end;
operator Explicit(const c_in: TRGBA16): TRGBA8; inline;
operator :=(const c_in: TRGBA16): TRGBA8; inline;
operator Explicit(const c_in: TRGBA8): TRGBA16; inline;
operator :=(const c_in: TRGBA8): TRGBA16; inline;
因此不需要帮助记录,也不会发生错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。