Search code examples
c++visual-studioenumsvisual-studio-2019

Compilation error using enum in Visual Studio


I defined this enum inside my class:

enum MenuNavigation : int {
    FileMenu,
    AutoAssign,
    EditMenu,
    ViewMenu,
    OptionsMenu,
    HelpMenu
};

When I compiled it I received a compilation error:

5>D:\My Programs\2019\MeetSchedAssist\Meeting Schedule Assistant\CreateReportDlg.h(360,7):
  error C2365: 'CCreateReportDlg::AutoAssign': redefinition; previous definition was 'enumerator'

5>D:\My Programs\2019\MeetSchedAssist\Meeting Schedule Assistant\CreateReportDlg.h(190):
  message : see declaration of 'CCreateReportDlg::AutoAssign'

On line 360 I have a function declaration:

BOOL AutoAssign(UINT uNumToFill, 
                UINT uStartIndex, 
                CStringArray &rAryStrAllBrothers, ROW_DATA_S &rsRowData, int iGridColumn);

If I rename my enumerator item as AutoAssignments or kAutoAssign it compiles.

I don't understand why a enum value which I understood has scope can't have the same name as a function defined in the parent class?


Solution

  • I don't understand why a enum value which I understood has scope can't have the same name as a function defined in the parent class?

    This is not true. enums with members having the same name will clash.

    What you should use instead is scoped enumerations, e.g.:

    enum class MenuNavigation : int {
        FileMenu,
        AutoAssign,
        EditMenu,
        ViewMenu,
        OptionsMenu,
        HelpMenu
    };
    

    and then MenuNavigation::AutoAssign.

    You should do the same thing for CCreateReportDlg, and then CCreateReportDlg::AutoAssign and MenuNavigation::AutoAssign would never clash.