error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::* )(v
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::* )(v

  1. #1
    Join Date
    May 2017
    Posts
    1

    Question error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::* )(v

    Hi
    I am trying to port old version C++ code into Visual Studio 2013. When I am trying to do this, I am getting the following error.

    1> INVOICEView.cpp
    1>INVOICEView.cpp(23): error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::* )(void)' to 'AFX_PMSG'
    1> None of the functions with this name in scope match the target type
    1>INVOICEView.cpp(23): fatal error C1903: unable to recover from previous error(s); stopping compilation
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Here is the code....

    ON_COMMAND(ID_SAVE_TO_HOST, OnSaveToHost)


    #define ON_COMMAND(id, memberFxn) \
    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
    static_cast<AFX_PMSG> (memberFxn) },
    // ON_COMMAND(id, OnBar) is the same as
    // ON_CONTROL(0, id, OnBar) or ON_BN_CLICKED(0, id, OnBar)



    int CINVOICEView::OnSaveToHost()
    {
    return (SaveToHost());
    }


    Any help to fix the above error would be appreciated. Please do let me know if you need any additional details. Thank you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,967

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    Quote Originally Posted by svsrkm View Post
    Here is the code....

    ON_COMMAND(ID_SAVE_TO_HOST, OnSaveToHost)


    #define ON_COMMAND(id, memberFxn) \
    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
    static_cast<AFX_PMSG> (memberFxn) },
    ...
    Why do you redefine the ON_COMMAND macro already defined by the framework?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    I guess, it's a copy paste from VC++'s standard headers ;-)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,967

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    Quote Originally Posted by AvDav View Post
    I guess, it's a copy paste from VC++'s standard headers ;-)
    Good point!
    Anyway, dear svsrkm, will you be so kind to post the declaration of your OnSaveToHost message handler?
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,183

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    Quote Originally Posted by svsrkm View Post
    [...]

    ON_COMMAND(ID_SAVE_TO_HOST, OnSaveToHost)


    #define ON_COMMAND(id, memberFxn) \
    { WM_COMMAND, CN_COMMAND, (WORD)id, (WORD)id, AfxSigCmd_v, \
    static_cast<AFX_PMSG> (memberFxn) },
    // ON_COMMAND(id, OnBar) is the same as
    // ON_CONTROL(0, id, OnBar) or ON_BN_CLICKED(0, id, OnBar)



    int CINVOICEView::OnSaveToHost()
    {
    return (SaveToHost());
    }
    When map a message by hand (not using the good wizard), MUST repect the required signature for the message handler!
    If you go to AFX_PMSG macro definition you'll see:
    Code:
    typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);
    This is a pointer to a member function that has no parameters and returns void type. Your handler function returns int. For that reason there is not possible to convert using static_cast.
    In older MFC versions, it was there a C-style cast, so compiler didn't coplain. That's very bad! The use of handlers with incorrect signature was a common generator of insidious bugs, many of them leading to application crashes randomly and only in the release builds.
    Last edited by ovidiucucu; May 24th, 2017 at 09:43 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    So, if summarize the solution for this particular issue, you need to either redefine a message handler macro as mentioned to set as:
    Code:
    typedef int (AFX_MSG_CALL CCmdTarget::*MYAFX_MSG)(void)
    Or consider to change your handler's returning type to void.
    I guess there are several other ways to check the correctness of the function call except for returning an integer value.
    Especially, from what I have guessed from the name of the function, the project is related to socket programming, and there are error handling routines available when dealing with WinSock systems' call (e.g.: WSAGetLastError()).
    Last edited by AvDav; May 24th, 2017 at 02:26 PM. Reason: typo

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,783

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    I am trying to port old version C++ code into Visual Studio 2013
    Why are trying to port 'old version code' to a 4-year old compiler? Why not port to the current version VS2017?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.4.0)

  8. #8
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    696

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    Quote Originally Posted by 2kaud View Post
    Why are trying to port 'old version code' to a 4-year old compiler? Why not port to the current version VS2017?
    I assume, this would cost big money. God bless Armenian internet, I can easily violate intellectual property rights from here. ;-)

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,183

    Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*

    Quote Originally Posted by AvDav View Post
    [...] you need to either redefine a message handler macro as mentioned to set as:
    Code:
    typedef int (AFX_MSG_CALL CCmdTarget::*MYAFX_MSG)(void)
    Bad idea!
    Returning an error code from VM_COMMAND message handler has NOT any sense. Just have a look in the MSDN documentation.
    The second choice is the right one:
    Quote Originally Posted by AvDav View Post
    Or consider to change your handler's returning type to void.
    I guess there are several other ways to check the correctness of the function call [...]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)