We help IT Professionals succeed at work.

GetMenu Error

artwilkes
artwilkes asked
on
I am trying to use a copy of COXMenuBar::GetMenu, some code from day before yesterday in a program I'm trying to update.  When Compiling I get several errors.
This one is first, and here is the error

\oxmenubar.h(612): error C2555: 'COXMenuBar::GetMenu': overriding virtual function return type differs and is not covariant from 'CWnd::GetMenu'

the code where this occurs is, and is from ultimate toolbox available from the code project.

      // --- In  :      
      // --- Out :
      // --- Returns:      Handle to the menu that is used in the menu bar
      // --- Effect : Retrieves handle to the menubar menu
                    //changed from HMENU
      HMENU GetMenu() const {
            ASSERT(::IsWindow(GetSafeHwnd()));
            return m_hMenu;
      }

What change do I need to make and is this going to work or should I start over?
I'll send the entire file if you would like?
This seems to work in vc 2005
Thanks
Comment
Watch Question

The error message suggest the problem is the return type, HMENU.  As I read it, the COXMenuBar is inheriting from CWnd, which also has a GetMenu function.  However, the CWnd::GetMenu returns something other than a HMENU.  Probably an object, maybe a CMenu, something that wraps a menu like CWnd wraps a window.  There's no conversion available from HMENU to whatever the base function returns.

The solution would be to change this function to return the same type as CWnd::GetMenu.  Obviously the member variable m_hMenu is a HMENU, with any luck there will be a constructor to initialise an instance of whatever type is needed with a HMENU.  Something like CMenu::CMenu (HMENU hMenu) function.

Chances are that MFC has changed between VC5 and VC10.

Author

Commented:
I think I have two options. One to change the function name in the library. or two to change the return type.
Yes, changing the function name would work too, I assumed you were intending to replace that function.    Though any other code inside the library that calls COXMenuBar::GetMenu will use CWnd::GetMenu instead.

You could call this GetMenuHandle or something, and change any calls to this function in the library as well.  It would be easy enough to find them because the compiler will complain that the calls are expecting the wrong return type.