VC6到VS2005中的转换(转)

VC6到VS2005中的转换(转)

这是我从网上搜集起来的,放到这里供自己参考,如有新的,请提供,谢谢!

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 catchcatch(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 VS2005VS2005中,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 VS20059.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);OnMyMessage返回值必须为LRESULT,其形式为:afx_msg LRESULT OnMyMessage(WPARAM, LPARAM);如果不符合,则有错误提示:error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”

在匹配目标类型的范围内没有具有该名称的函数

error C2440: “static_cast”: 无法从“void (__thiscall CPppView::* )(void)”转换为“LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)”

在匹配目标类型的范围内没有具有该名称的函数

b. ON_COMMAND_EX(id,OnMyMessage2);

在VS2005中,OnMyMessage返回值必须为BOOL,且含有一个 UINT 参数指出了命令ID,其形式为:afx_msg BOOL OnMyMessage(UINT);如果不符合,

则有错误提示,如在VS6中,OnMyMessage2的定义为afx_msg BOOL OnViewZoomBar()时亦可正常编译通过,但在VS2005下,有错误提示:

error C2440: “static_cast”: 无法从“BOOL (__thiscall CMainFrame::* )(void)”转换为“BOOL (__thiscall CCmdTarget::* )(UINT)”

在匹配目标类型的范围内没有具有该名称的函数

 error C2440: “static_cast”: 无法从“BOOL (__thiscall CMainFrame::* )(void)”转换为“BOOL (__thiscall CCmdTarget::* )(UINT)”

在匹配目标类型的范围内没有具有该名称的函数

13. 字符处理

 在c中广泛使用的strcpy,strcat,strstr等等推荐使用更为安全的strcpy_s,strcat_s,strstr_s等来代替

14. 数学函数检查

 VS2005中,数学函数的参数检查更为严格,如pow(2, 45)会引起一个错误提示如下:

error C2668: “pow”: 对重载函数的调用不明确

d:\program files\microsoft visual studio 8\vc\include\math.h(575): 可能是“long double pow(long double,int)”

d:\program files\microsoft visual studio 8\vc\include\math.h(527): 或“float pow(float,int)”

d:\program files\microsoft visual studio 8\vc\include\math.h(489): 或“double pow(double,int)”

试图匹配参数列表“(int, int)”时

正确的使用为pow(2.0, 45)

15. 更加符合C++标准

如在VS6中,在FOR循环中的循环变量的定义的作用域延伸到循环体外,VS2005则修正了这样的bug。

VC6:

for(int i=0;i<100;i++)f2();

for(i = 1;i<10;i++)f1(); //i已经定义

而有VS2005中,第二句的i必须重新定义。16. A WM_COMMAND handler has always the following signaturevoid 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 parameters19.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.本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/k2eats/archive/2008/06/19/2566566.aspx

本文来自博客园文章出处:http://www.cnblogs.com/papo/archive/2009/04/25/1552121.html

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jianglike18/archive/2009/09/25/4593684.aspx

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/TRACY1986/archive/2009/11/08/4785018.aspx

posted on 2011-05-08 01:56  sunliming  阅读(4106)  评论(0编辑  收藏  举报