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.
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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?
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
I guess, it's a copy paste from VC++'s standard headers ;-)
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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?
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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.
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()).
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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?
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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. ;-)
Re: error C2440: 'static_cast' : cannot convert from 'int (__thiscall CINVOICEView::*
Quote:
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:
Quote:
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 [...]