1

I am having a peculiar problem with forward declared enums in my application. So, when trying to compile my C++ code, I need to include a couple of include files, where in the enums have been forward declared. For example, FwdDeclarations.h:

//-----------------------------------------------------------------------------
//! Forward declaration of the error code enumeration.
//-----------------------------------------------------------------------------
typedef enum EErrorCode ErrorCode_t;

This enum is in-turn declared in another header file, say, ActImpl.h

enum EErrorCode 
{
    E_NO_ERROR,
    E_SOME_ERROR
}

Now to get my code compiling I tried fixing the forward declared enums based on what was suggested in this question. So, more like, now FwdDeclarations.h has:

#ifdef __cplusplus
enum EErrorCode  : int64_t ;
#endif
//-----------------------------------------------------------------------------
//! Forward declaration of the error code enumeration.
//-----------------------------------------------------------------------------
typedef enum EErrorCode ErrorCode_t;

This triggers another error error: underlying type mismatch in enum 'enum EErrorCode', which stems from another issue based on the enum underlying types.

Since this a unit test application, I would like to not modify contents of either FwdDeclarations.h or ActImpl.h. How could I compile my code, with these forward declared enums?

Frant
  • 5,382
  • 1
  • 16
  • 22
mmcblk1
  • 158
  • 1
  • 3
  • 10
  • 1
    `in another header file, say, ActImpl.h` Include that header first. – KamilCuk Dec 15 '21 at 18:57
  • 1
    forward declaring an `enum` is new to me .. is there a reason you can't/won't just include the header file where it's defined? – yano Dec 15 '21 at 19:01
  • @KamilCuk That indeed worked! Is there any other work around to avoid changing the `FwdDeclarations.h` file? – mmcblk1 Dec 15 '21 at 19:10
  • @yano This seems to be our API includes. So modifying them is out of scope for now. – mmcblk1 Dec 15 '21 at 19:11
  • Your question is really about C/C++ interoperability of enums. See: [Forward declare C enum](https://stackoverflow.com/questions/7480217/c-forward-declaration-of-enums). The syntax seems the same, but the C++ versions have added a lot of machinery behind the scenes. If you are trying to make them inter-operable you probably need to use limited C++ mechanics. Probably it is better to create a class that can convert the C enum to C++ behaviours using static methods and having the C enum data member. The core of the matter is that the size of the enum may change depending on the item count. – artless noise Dec 16 '21 at 17:19

0 Answers0