This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

copy assignment not allowed in union

I am attempting to do some development using the usbd_hid_generic example peripheral code. I am using some C++ libraries in this project, however there is syntax in the usbd code (specifically in the app_usbd_class_base.h file) that fails to compile. This is making it difficult for me to be able to integrate my new code with the old code. To simplify things, I will just outline changing the main.c file to main.cpp in the base example program and describe what happens. If this can be fixed, I should be able to work out the rest. I am using gcc-arm-none-eabi for compilation on a Linux OS and I am using SKD v. 13, though I have also tried this under v. 14 with the same results.

Step 1: Rename main.c to main.cpp and change the corresponding entry in the Makefile.

Step 2: Compile the code - fails.

The first error is: [...]/app_usbd_class_base.h:674:5: sorry, unimplemented: non-trivial designated initializers not supported

According to the following, it seems it should be possible to compile using older versions of C++, but designated initializer lists are not supported in current C++ standards. stackoverflow.com/.../why-does-c11-not-support-designated-initializer-lists-as-c99

I attempted a workaround by setting -std=c++98, but that resulted in another error.

There is an additional error: [...]/app_usbd_class_base.h:586:11: error: member 'app_usbd_hid_mouse_u:: app_usbd_hid_mouse_u::specific' with copy assignment operator not allowed in union.

Possible options:

I am looking for how to integrate the usbd libraries within a C++ project. If people have some insights, please share. So far, I figure some workarounds are for me to set up code to implement the C++ libraries I am working with in C, or I can write access functions to wrap C++ code in extern "C" blocks and leave the usbd code calls in a C context. I think I will try the second option first, but it seems that it should be more straightforward to use Nordic usbd libraries within C++ projects.

Parents Reply
  • C++ cannot skip definitions in between. All definitions above the one that causes the error has to be filled in order. You can however skip all bellow. For example :

    static const app_usbd_config_t usbd_config = {
        .ev_state_proc = usbd_user_ev_handler
    };
    

    Will cause the error but :

    static const app_usbd_config_t usbd_config = {
        .ev_isr_handler = NULL,
        .ev_state_proc = usbd_user_ev_handler
    };
    

    will not.

    for union stuff :

    typedef struct a {
    	int z;
    	union {
    		int b;
    		struct {
    			uint8_t c;
    			uint16_t d;
    		} f;
    	};
    } A;
    
    A x = {
    	.z = 0,
    	{ .f = {.c = 1, .d = 2} }
    };
    
    A y = {
    	.z = 0,
    	{ .b = 3 }
    };
    
Children
No Data
Related