错误现象之三: f:project.....WzButton.cpp(74) : error C2440: 'static_cast' : cannot convert from 'UINT (__thiscall CWzButton::* )(CPoint)' to 'LRESULT (__thiscall CWnd::* )(CPoint)' Cast from base to derived requires dynamic_cast or static_cast 出现这个错误的原因可是“人力不可抗拒”之原因造成的,因为旧版本的 ON_WM_NCHITTEST 宏使用了 UINT (__thiscall CWzButton::* )(CPoint); 类型的类成员函数指针,其定义如下: #define ON_WM_NCHITTEST() { WM_NCHITTEST, 0, 0, 0, AfxSig_wp, (AFX_PMSG)(AFX_PMSGW)(UINT (AFX_MSG_CALL CWnd::*)(CPoint))&OnNcHitTest }, 但是新版本变成了: #define ON_WM_NCHITTEST() { WM_NCHITTEST, 0, 0, 0, AfxSig_l_p, (AFX_PMSG)(AFX_PMSGW) (static_cast< LRESULT (AFX_MSG_CALL CWnd::*)(CPoint) > (&ThisClass :: OnNcHitTest)) }, 注意返回值类型由UINT改成了LRESULT,再加上static_cast的严格检查,所以就出错了。修改的方法就是将你的OnNcHitTest函数由: afx_msg UINT OnNcHitTest(CPoint point); 改成: afx_msg LRESULT OnNcHitTest(CPoint point); 不必太在意,这个不是你的错,不过,如果你要维护一个老的界面库(通常很多控件的subclass都会用到ON_WM_NCHITTEST),改起来还是很痛苦地,不扯了,继续下一个。 七、statreg.cpp 和 atlimpl.cpp 的废弃(obsolete)问题 在编译老的ATL向导生成的代码时,会遇到下面的编译输出: StdAfx.cpp statreg.cpp is obsolete. Please remove it from your project. atlimpl.cpp is obsolete. Please remove it from your project. 因为老的ATL向导生成的代码通常在stdafx.cpp文件中添加以下代码: #ifdef _ATL_STATIC_REGISTRY #include #include #endif #include 根据提示删除#include 和#include 两行代码就行了,不过更好的办法是这样改: #ifdef _ATL_STATIC_REGISTRY #include #if _MSC_VER <= 1200 // MFC 6.0 or earlier #include #endif #endif #if _MSC_VER <= 1200 // MFC 6.0 or earlier #include #endif 八、新的C++编译器不再支持默认类型的变量定义 错误现象是: f:project.....WzCheckBox.cpp(464) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 产生这个错误的原因是程序中出现了这样的代码: const some_const_var = 10; 或 static some_static_bool = FALSE; 新的C++编译器严格按照C++标准,不再支持默认类型的变量定义方式,必须严格指定变量类型,如下使用: const int some_const_var = 10; 或 static BOOL some_static_bool = FALSE; 九、for 语句的变量作用域问题 考察下面的代码: for(int i = 0; i < 120; i++) { if(something_happen) { break; } ............. } if(i < 120) { //something happen } |
|