0

I'm feeling really dumb right now, because it's been almost 2 years since I've done any C/C++, while my job has had me writing Java for that time and now I'm having trouble remembering even simple stuff, or else I remember the way to do it in Java instead... I have some C code that I'm trying to use in C++, but I'm having issues with the few things that aren't directly compatible, like struct initialization. The struct definition looks like this:

typedef struct
{
    struct
    {
        uint8_t  StreamingInterfaceNumber;
        USB_Endpoint_Table_t DataINEndpoint;
        USB_Endpoint_Table_t DataOUTEndpoint;
    } Config;

    struct
    {
        uint8_t RESERVED;
    } State;
} USB_ClassInfo_MIDI_Device_t;

The original C-style initialization looks like this:

USB_ClassInfo_MIDI_Device_t MIDI_Interface =
{
    .Config =
        {
            .StreamingInterfaceNumber = 1,
            .DataINEndpoint           =
                {
                    .Address          = MIDI_STREAM_IN_EPNUM,
                    .Size             = MIDI_STREAM_EPSIZE,
                    .Banks            = 1,
                },
            .DataOUTEndpoint           =
                {
                    .Address          = MIDI_STREAM_OUT_EPNUM,
                    .Size             = MIDI_STREAM_EPSIZE,
                    .Banks            = 1,
                },
        },
};

Which obviously doesn't work in C++. What's the best way to initialize a struct in C++? I don't want to modify the definition, since it exists in a code library that I don't maintain, and would like to leave alone, so I'd rather not add a constructor. I like the style suggested here https://stackoverflow.com/a/6182627/1609411 but I don't know what the syntax is supposed to look like for the nested structs.

1 Answer 1

1

I'm pretty sure that the style in that linked answer just involves commenting out all of the .BLAH = sections. As long as you have all of the fields in the right order, it will initialize fine.

USB_ClassInfo_MIDI_Device_t MIDI_Interface =
{
    /* .Config = */
        {
          /* .StreamingInterfaceNumber = */ 1,
          /* .DataINEndpoint           = */
                {
                    /* .Address          = */ MIDI_STREAM_IN_EPNUM,
                    /* .Size             = */ MIDI_STREAM_EPSIZE,
                    /* .Banks            = */ 1,
                },
          /* .DataOUTEndpoint           = */
                {
                    /* .Address          = */ MIDI_STREAM_OUT_EPNUM,
                    /* .Size             = */ MIDI_STREAM_EPSIZE,
                    /* .Banks            = */ 1,
                },
        },
};
Sign up to request clarification or add additional context in comments.

5 Comments

I tried that but it gives me a compiler error "redefinition of 'USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface'"
@qwertymodo Where is it giving you that error? That doesn't look like something in this code.
I typed exactly what you had there, and the error is on the first line USB_ClassInfo_MIDI_Device_t Keyboard_MIDI_Interface = The error references the original definition of the struct type and seems to think I'm redefining it here instead of instantiating it >.<
Hmm... maybe it's because I'm doing this in the global scope?
I think, it happens because you define variable MIDI_Interface in h-file and include it in more then one source. Move definition into cpp-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.