1. 程式人生 > >error C2440: “static_cast”: 無法從“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)

error C2440: “static_cast”: 無法從“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)

目標 div line rdquo ria 升級 ret error lpctstr

轉自原文 error C2440 “static_cast” 無法從“void (__thiscall C* )(void)...

error C2440: “static_cast”: 無法從“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)。

開發平臺由VC6.0升級至VS2005以上的版本,需要將原有的項目遷移,可能碰到類似錯誤:error C2440: “static_cast” 無法從“void (__thiscall C* )(void)...

舊版本的C++工程升級後出現的該問題,VS2005之後的版本對消息的檢查更為嚴格,以前在VC6下完全正常運行的消息映射在VS2005下編譯不通過。

ON_MESSAGE(WM_message,OnMyMessage);  

OnMyMessage返回值必須為LRESULT,其形式為:afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);如果不符合,則有錯誤提示:
error C2440: “static_cast”: 無法從“void (__thiscall  CXXX::* )(WPARAM,LPARAM)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)” 在匹配目標類型的範圍內沒有具有該名稱的函數  
error C2440: “static_cast”: 無法從“
void (__thiscall CXXX::* )(void)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”在匹配目標類型的範圍內沒有具有該名稱的函數

解決方法如下:

首先,把原來的消息函數返回值類型改為LRESULT,函數內可以隨便寫個return TRUE;

然後消息函數的參數必須改寫成(WPARAM wParam,LPARAM lParam)而不論這兩個參數是否用得到;

最後,消息映射如ON_MESSAGE(WM_message,& OnMyMessage)。

error C2440: “static_cast”: 無法從“LRESULT (__thiscall CTextProgressCtrl::* )(UINT,LPCTSTR)”轉換為“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)