VC6到VS2005中的转换 转
原文见:http://hi.baidu.com/chris_cui/blog/item/f9b79bf556b89ee17609d75e.html
这是我从网上搜集起来的,放到这里供自己参考,如有新的,请提供,谢谢!
1、 error C2668: 'sqrt' : ambiguous call to overloaded function
在VS2005中存在sqrt函数的重载。当编译器看到sqrt(int)时,找不到相应的函数,此时存在sqrt(float)和sqrt(long double)两个函数,编译器不知道程序员需要哪个函数,就会出现错误。可以使用sqrtf( )代替。
2、 error C2039: 'ReadHuge' : is not a member of 'CFile
VS2005中,readhuge被read包括了。
3、 error C2668: 'pow' : ambiguous call to overloaded function
在VS2005中,需要写成pow( (double)i, 2),原因我没有查到。其函数原型为 double __cdecl pow(__in double _X, __in double _Y); 在VC6中,函数原型为 _CRTIMP double __cdecl pow (double, double);
4. 以前可以这样用try catch
catch(CException *e)
{
pApp->Warn("%s",e->GetErrorMessage);
e->Delete();
return FALSE;
}
现在必须修改为:
catch(CException *e)
{
TCHAR errormsg[255];
e->GetErrorMessage (errormsg,255,NULL);
pApp->Warn("%s",errormsg);
e->Delete();
return FALSE;
}
5. strchr必须强制转换一下。
以前可以 char *str2=strchr(line,'|');
2005必须 char *str2=(char *)strchr(line,'|');
6. lifescope of int i in for(int i; i< size; ++i)in VC6,
the codes below are ok
for(int i = 0; i< 10; ++i)
{
//...
}
for(i = 20; i< 40;++i)
{
//...
}
but in VS2005, we should write like below:
for(int i = 0; i< 10; ++i)
{
//...
}
for(int i = 20; i< 40;++i)
{
//...
}
in fact, from vs.net, the compiler accord with C++ standard more than VC6.
If you are porting a VC6 project to VS2005, you will encounter many many codes like this.
7. ON_WM_NCHITTEST (and other MFC macros) won't compile in VS2005
VS2005中,ON_WM_NCHITTEST宏编译不过
When I add a message handler of ON_WM_NCHITTEST to a CControlbar-derived class, it compiles error:
error C2440: 'static_cast' : cannot convert from 'UINT (__thiscall CMenuBar::* )(CPoint)' to 'LRESULT (__thiscall CWnd::* )(CPoint)' Cast from base to derived requires dynamic_cast or static_cast
To fix this bug, we should change the prototype of OnNcHitTest
from
afx_msg UINT OnNcHitTest(CPoint point);
to
afx_msg LRESULT OnNcHitTest(CPoint point);
8. VS2005中有些可能引起内存越界的函数不建议使用了
In VS2005, some dangerous functions are deprecated
char c[10];
strcpy(c, "testtestts"); //ok with VC6, but not in VS2005
strcpy_s(c, _countof(c),"testtestt");//9 chars, ok in VS2005
strcpy_s(c, _countof(c),"testtestt");//10 chars, assert!!!!! in VS2005
9.error C2440: 'static_cast' : cannot convert from 'HRESULT (__thiscall CtestpalView::* )(WPARAM,LPARAM)' to 'AFX_PMSG'
None of the functions with this name in scope match the target type
HRESULT (__thiscall CtestpalView::* )(WPARAM,LPARAM)
AFX_PMSG类型:
void (AFX_MSG_CALL CCmdTarget::* )(void)
10. error C2440: 'static_cast' : cannot convert from 'void (__thiscall CSettingStart::* )(BOOL,HANDLE)' to 'void (__thiscall CWnd::* )(BOOL,DWORD)'
In VC6, the handler for ON_WM_ACTIVATEAPP was expected to be
afx_msg void OnActivateApp( BOOL, HANDLE);
In VC7 and vs2005, it has been changed to
afx_msg void OnActivateApp( BOOL, DWORD );
11.error LNK2019: unresolved external symbol "wchar_t * __stdcall _com_util::Co.....
解决方法,
Property page ->C/C++ ->Language ->treat Wchar-t 改为 No
a. ON_MESSAGE(message,OnMyMessage);
b. ON_COMMAND_EX(id,OnMyMessage2);
13. 字符处理
14. 数学函数检查
15. 更加符合C++标准
16. A WM_COMMAND handler has always the following signature
void OnFunc();
17. A message handler has always this signature
LRESULT OnMsgHandler(WPARAM wparam, LPARAM lparam);
So you have to write 2 handlers:
The map entries:
ON_COMMAND(ID_BTN_ASINCRONICO, OnBtnAsincronico)
ON_MESSAGE(WM_APP_NOSINCRONICO, OnBtnAsincronico)
The header file declaration:
afx_msg LRESULT OnBtnAsincronico(WPARAM wparam, LPARAM lparam);
afx_msg void OnBtnAsincronico();
The definition:
LRESULT CMainFrame:nBtnAsincronico(WPARAM /*wparam*/, LPARAM /*lparam*/)
{
OnBtnAsincronico();
return 0;
}
void CMainFrame:nBtnAsincronico()
{
...
}
18.CDlg *dlg=new CDlg;
dlg->create(IDD_DLG,this);//出错之处
error C2660: 'Create' : function does not take 2 parameters
且我将第二个参数去掉的时候,又会显示
error C2660: 'Create' : function does not take 1 parameters
19.error C2871: 'System' : a namespace with this name does not exist
这个错误只能说VC编译器还不够智能啊
在使用前需要使用Common Language Runtime Support (/clr).
在配置属性中,选择general-》选择clc (Configuration Properties/General/Common Language Runtime support)
解释:This is not C++ but Managed C++ (clr) and thus needs to be specified in project properties at Configuration Properties/General/Common Language Runtime support. There you will see drop down box with options.
浙公网安备 33010602011771号