【问题标题】:Invalid operands to binary expression on TGridOptionsTGridOptions 上二进制表达式的无效操作数
【发布时间】:2015-09-02 13:51:28
【问题描述】:

我最近从 C++ Builder XE8 升级到 Rad Studio 10 Seattle。我正在尝试使用新的 Clang 编译器,但遇到了问题。

在自定义网格类中,我有以下代码行:

__property Options = {default=TGridOption::AlternatingRowBackground << TGridOption::RowSelect};

这会导致编译器出现以下错误:

[CLANG Error] FmGridU.h(57): invalid operands to binary expression ('Fmx::Grid::TGridOption' and 'Fmx::Grid::TGridOption')

根据我在其他问题中读到的内容,我需要做一些事情,比如实现我自己的 &lt;&lt; 运算符。但是,我不确定我将如何去做。据我了解,当前代码是使用控制选项的标准方式。

新的 Clang 编译器有什么不同导致它抛出经典 Boreland 编译器没有的错误?如何实现 &lt;&lt; 运算符以允许我设置 options 属性?

编辑:

我已按照 Remy 的建议更正了我的语法。

__property Options = {default = TGridOptions() << TGridOption::AlternatingRowBackground << TGridOption::RowSelect};

但是,现在我收到以下错误: 'expression is not an integral constant expression'

根据this question,答案是将代码放在函数中。但是,由于我在头文件中声明了这个属性,所以我不知道该怎么做。我还有什么遗漏的吗?

【问题讨论】:

  • other question 与 C++Builder 编译器或属性无关。
  • @RemyLebeau 对。由于错误是相同的,我认为它至少可以引导我朝着正确的方向前进。

标签: c++ clang c++builder firemonkey c++builder-10-seattle


【解决方案1】:

在经典编译器或新的 CLang 编译器中,这不是有效的语法。 OptionsTGridOptions,它是 Set&lt;&gt;TGridOption 值(即:typedef System::Set&lt;TGridOption, TGridOption::AlternatingRowBackground, TGridOption::HeaderClick&gt; TGridOptions;)。您需要先构造一个实际的TGridOptions 对象,然后才能为其分配任何值,例如:

TGridOptions MyOptions = TGridOptions() << TGridOption::AlternatingRowBackground << TGridOption::RowSelect;

但是,您不能在属性声明中创建 Set&lt;&gt; 对象。 不过,您可以做的是指定一个数字常量,该常量表示Set&lt;&gt; 对象的二进制内容。在这种情况下,对于TGridOptions 集合,TGridOption::AlternatingRowBackground 位于第 0 位,TGridOption::RowSelect 位于第 7 位,因此同时包含TGridOption::AlternatingRowBackgroundTGridOption::RowSelect 启用的集合的数值是二进制@ 987654335@,十六进制0x81,十进制129,因此您可以这样声明属性:

__property Options = {default = 0x81};

__property Options = {default = 129};

这在 Delphi 中比在 C++ 中更容易处理,因为 Delphi 允许您指定实际集合(Delphi 编译器在生成 C++ .HPP 文件时将其转换为数字常量):

property Options default [TGridOption.AlternatingRowBackground, TGridOption.RowSelect];

在任何一种情况下,与任何其他属性一样,请确保您实际上在网格的构造函数中分配了相同的 TGridOptions 默认值以匹配属性声明,否则该属性将不会流入/流出 DFM/ FMX 资源正确。在这种情况下,您可以使用真正的TGridOptions 对象来分配属性值:

__fastcall TMyGrid::TMyGrid(TComponent *AOwner)
    : public TCustomGrid(AOwner)
{
    Options = TGridOptions() << TGridOption::AlternatingRowBackground << TGridOption::RowSelect;
}

【讨论】:

  • 使用__property Options = {default = TGridOptions() &lt;&lt; TGridOption::AlternatingRowBackground &lt;&lt; TGridOption::RowSelect}; 给了我错误'expression is not an integral constant expression' 我错过了什么吗?
  • 那么你将不得不使用一个数字常量。如果您查看具有基于Set 的属性和默认值的各种VCL/FMX 头文件,它们都使用数字常量(在生成头文件时由Delphi 编译器翻译)。我已经更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多