LWein
LWein

Reputation: 21

compiler error: invalid use of undefined type 'union ...' . What is wrong in the definition?

My code is organized as following:

File: main.c

 #include "init.h"
 #include "SpiComm.h"
 //. . . . 

File: init.h

typedef union {

    unsigned int wholeReg;

    struct   {
        unsigned CMD1_flag: 1; 
            . . . .

    };

}  DISCRETE_FLAGS_REG_TYPE;

extern volatile union DISCRETE_FLAGS_REG_TYPE DiscreteFlagsReg; //register declaration

File: SpiComm.c

#include "SpiComm.h"

volatile union DISCRETE_FLAGS_REG_TYPE  DiscreteFlagsReg;


unsigned char* SpiTxFrameSetup (void)
{
            . . . .                
    SpiTxMsgByte1.dt1CmdFlag = L_DiscreteFlagsReg.regBits.DT1_HW_CMD_flag;
            . . . . 
    spiTxMsg[4] = SpiTxMsgByte1.byte0Full;
        . . . . 
    return (spiTxMsg);  //return the pointer to Tx message array;
}

Compiler is notifying this for SpiComm.c file (and for any other .c file that attempts to use any field of this union):

error: invalid use of undefined type 'union DISCRETE_FLAGS_REG_TYPE'

What is wrong? Why other c files doesn't recognize the union type I've defined in init.h?

Upvotes: 1

Views: 2065

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

The union itself has no name; "DISCRETE_FLAGS_REG_TYPE" is a typedef name.

You can use the typedef name:

extern volatile DISCRETE_FLAGS_REG_TYPE DiscreteFlagsReg;

or name the union:

union DISCRETE_FLAGS_REG_TYPE
{
   /* ... */
};

and use union DISCRETE_FLAGS_REG_TYPE in C, or DISCRETE_FLAGS_REG_TYPE in C++

or, in C, combine the naming and the typedef:

typedef union DISCRETE_FLAGS_REG_TYPE
{
   /* ... */
} DISCRETE_FLAGS_REG_TYPE;

(The last variant is an error in C++.)

Upvotes: 3

Related Questions