|
-
May 17th, 2017, 01:47 PM
#1
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.
-
May 17th, 2017, 02:59 PM
#2
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
 Originally Posted by svsrkm
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
-
May 18th, 2017, 07:46 AM
#3
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
I guess, it's a copy paste from VC++'s standard headers ;-)
-
May 18th, 2017, 07:53 AM
#4
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
 Originally Posted by AvDav
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
-
May 24th, 2017, 09:39 AM
#5
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
 Originally Posted by svsrkm
[...]
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.
-
May 24th, 2017, 01:01 PM
#6
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
-
May 24th, 2017, 01:25 PM
#7
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 24th, 2017, 02:11 PM
#8
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
 Originally Posted by 2kaud
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. ;-)
-
May 25th, 2017, 04:54 AM
#9
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
 Originally Posted by AvDav
[...] 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:
 Originally Posted by AvDav
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 [...]
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|