3

I have a snippet of code which compiles in C++ Builder XE8 using the classic BCC compiler. However, in Rad Studio 10 Seattle using the Clang compiler I get the error

'no matching constructor found for initialization of TChoiceItem'

Here is the snippet of code which causes the error.

LISTITEM_BEGIN( sch_TYPE_Choice )
    LISTITEM_DATA( sch_TYPE_Daily,      "Daily" )
    LISTITEM_DATA( sch_TYPE_Weekly,     "Weekly" )
    LISTITEM_DATA( sch_TYPE_Monthly,    "Monthly" )
LISTITEM_END()

Here is the code which defines TChoiceItem

//------------------------------------------------------------------------------
#define LISTITEM_BEGIN( Name ) TChoiceItem Name[] = {
//------------------------------------------------------------------------------
#define INT_LISTITEM_BEGIN( Name ) TIntChoiceItem Name[] = {
//------------------------------------------------------------------------------
#define LISTITEM_DATA( XCode, XText ) { XCode, 0, (char*)XText, 0 },
#define LISTITEM_DATA_NC( XShortText, XText ) { 0, (char*)XShortText, (char*)XText, 0 },
#define LISTITEM_DATA_EX( XCode, XShortText, XText ) { XCode, (char*)XShortText, (char*)XText, 0 },
#define LISTITEM_DATA_EX2( XCode, XShortText, XText, XDesc ) { XCode, (char*)XShortText, (char*)XText, (char*)XDesc },
#define LISTITEM_END() LISTITEM_DATA(0,0) };

I am fairly new to C++ so I am not exactly sure what to call the above method of defining a class/method.

Is this some sort of dated language feature not supported by the Clang compiler? Is there a way to modify the code or definition so the compiler will accept it?

Edit:

I found the actual declaration of the TChoiceItem class.

class TChoiceItem : public TChoiceBase
{
    public:
        char  Code;
        char *ShortText;
        char *Text;
        char *Desc;
};

It does't appear to have any sort of standard constructor at all. But somehow, everything still compiles and works with the classic BCC compiler.

Edit 2:

I found this question which looks to be describing a similar issue. Could it be that I need to include some kind of compiler flag when compiling the code? If so can I add a flag somehow in the embarcadero project compiler settings?

4

1 回答 1

2

使用大括号中的值列表来初始化类或结构的各个成员称为聚合初始化

正如 cppreference.com 上所解释的,如果类具有基类(以及其他限制),则不允许聚合初始化。 TChoiceItem继承自TChoiceBase,因此不允许聚合初始化(并且“经典” bcc32 编译器不应该允许它)。

你有几个选择:

首先,您可以将代码更改为不继承自TChoiceBase.

其次,您可以定义一个构造函数:

TChoiceItem(char code, char *short_text, char *text, char *desc)
    : Code(code), ShortText(short_text), Text(text), Desc(desc) {}

C++11 的统一初始化意味着你的宏的语法不必改变:大括号不是表示单个成员的值列表,而是表示构造函数的参数列表,但结果是相同的.

于 2015-09-03T17:37:46.580 回答