简体   繁体   中英

Invalid operands to binary expression on TGridOptions

I have recently upgraded from C++ Builder XE8 to Rad Studio 10 Seattle. I am trying to use the new Clang compiler but I am running into an issue.

On a custom grid class I have the following line of code:

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

Which causes the following error from the compiler:

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

From what I have read in other questions, I need to do something like implement my own << operator. However, I am not exactly sure how I would go about doing this. From my understanding, the current code is the standard way to work with control options.

What is the difference with the new Clang compiler that causes it to thrown an error where the Classic Boreland compiler does not? How can I implement the << operator to allow me to set the options property?

Edit:

I have corrected my syntax as per Remy's suggestion.

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

However, now I get the following error: 'expression is not an integral constant expression'

According to this question the answer was to put the code inside of a function. However, since I am declaring this property in a header file, I am not sure how to do that. Is there something else I am missing?

That is not valid syntax, in either the classic compiler or the new CLang compiler. Options is a TGridOptions , which is a Set<> of TGridOption values (ie: typedef System::Set<TGridOption, TGridOption::AlternatingRowBackground, TGridOption::HeaderClick> TGridOptions; ). You need to construct an actual TGridOptions object before you can assign any values to it, eg:

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

However, you cannot create a Set<> object inside of a property declaration. What you can do, though, is specify a numeric constant that represents the binary content of a Set<> object. In this case, for a TGridOptions set, TGridOption::AlternatingRowBackground is located at bit 0 and TGridOption::RowSelect is located at bit 7, thus the numeric value of the set that contains both TGridOption::AlternatingRowBackground and TGridOption::RowSelect enabled is binary 10000001 , hex 0x81 , decimal 129 , thus you can declare the property like this:

__property Options = {default = 0x81};

__property Options = {default = 129};

This is easier to handle in Delphi than in C++, as Delphi lets you specify the actual set (which the Delphi compiler translates to a numeric constant when generating a C++ .HPP file):

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

In either case, as with any other property, make sure that you are actually assigning the same TGridOptions default value in your grid's constructor to match the property declaration, or else the property will not stream to/from a DFM/FMX resource correctly. In this case, you can use a real TGridOptions object to assign the property value:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM