This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Struct Enum support for Keil compiler

Hi,
Need information on support of scoped enums in Keil compiler.
My final objective is to use the Enums as AIAction:Enter</i or AIAction:Exit instead of plain Enter or Exit in the source code C files.
This is to solve the global namespace pollution.

I have the following structure in header file :

struct AIAction{
    enum AIActionType
    {
        Enter,
        Exit,
        Run,
    };
};

For using it, following is the code in C file (filetype is C)

    int iType;
    iType = AIAction::Run;

When I compile this with current settings, it is giving me error #618: struct or union declares no named members

However, if I change the filetype to C++, it compiles successfully. So basically C++ compiler is allowing such structure definition.

From this, I infer that C has got limitation of not allowing any non-named members in structures.

How do I use the enums the way I want? Can this be achieved without using C++ compiler?

Parents
  • My final objective is to use the Enums as AIAction::Enter or AIAction::Exit instead of plain Enter or Exit in the source code C files.

    [Note: some typos corrected in the above]

    That objective is impossible, because those idioms cannot do not exist in C. Files that contain such idioms are, by definition, not C source files; they're C++ source files.

    From this, I infer that C has got limitation of not allowing any non-named members in structures.

    You inferred incorrectly. The problem is not that this is an unnamed member. The problem is that the thing you're trying to define is not actually a member of the struct at all --- it just becomes part of its namespace scope. The error messages that lead you to that incorrect conclusion is based on the fact that this is C++-only syntax, which led the C compiler totally astray. BTW C does allow unnamed members, as of the latest revision of the Standard in 2011, but those must be structs or unions themselves.

    There are people who claim to be teaching a programming language by the name of "C/C++". Well, sorry, but that language does not exist, and it never really did. C and C++ are different languages. They do have a common subset, but that's become smaller and less useful with each of the last decade or two's worth of updates to the both language definitions. So you'll have to make up your mind which of them you're going to use, and stick with that decision.

Reply
  • My final objective is to use the Enums as AIAction::Enter or AIAction::Exit instead of plain Enter or Exit in the source code C files.

    [Note: some typos corrected in the above]

    That objective is impossible, because those idioms cannot do not exist in C. Files that contain such idioms are, by definition, not C source files; they're C++ source files.

    From this, I infer that C has got limitation of not allowing any non-named members in structures.

    You inferred incorrectly. The problem is not that this is an unnamed member. The problem is that the thing you're trying to define is not actually a member of the struct at all --- it just becomes part of its namespace scope. The error messages that lead you to that incorrect conclusion is based on the fact that this is C++-only syntax, which led the C compiler totally astray. BTW C does allow unnamed members, as of the latest revision of the Standard in 2011, but those must be structs or unions themselves.

    There are people who claim to be teaching a programming language by the name of "C/C++". Well, sorry, but that language does not exist, and it never really did. C and C++ are different languages. They do have a common subset, but that's become smaller and less useful with each of the last decade or two's worth of updates to the both language definitions. So you'll have to make up your mind which of them you're going to use, and stick with that decision.

Children