clangc++builderc++builder-xe8c++builder-10-seattle

'No matching constructor for initialization' Rad Studio 10 Clang Compiler


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?


Solution

  • Using a list of values in braces to initialize the individual members of a class or struct is known as aggregate initialization.

    As explained on cppreference.com, aggregate initialization isn't permitted if the class has a base class (among other restrictions). TChoiceItem inherits from TChoiceBase, so aggregate initialization isn't allowed (and the "classic" bcc32 compiler shouldn't have allowed it).

    You have a couple of choices:

    First, you can change the code to not inherit from TChoiceBase.

    Second, you can define a constructor:

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

    C++11's uniform initialization means that your macros' syntax doesn't have to change: instead of braces meaning a list of values for individual members, the braces will mean a list of parameters to the constructor, but the result will be the same.