当前位置:编程学习 > VC++ >>

VC6.0转VS2005问题及解决办法

我对原文的内容加以整理,便于阅读,以后发现新的转换问题,将会继续补充,

也欢迎大家跟帖提出新的问题并解决办法,帮助我继续完善,谢谢!


一.  函数原型改变导致的问题

1. errorC2440:CMainFrame 无法从 NMTOOLBARA 转换为 NMHDR

    VC6下OnToolbarDropDown的函数原型:

[cpp] 
voidCMainFrame::OnToolbarDropDown(NMTOOLBAR* pnmtb, LRESULT *plr)  
   需要改为:
[cpp] 
void CMainFrame::OnToolbarDropDown(NMHDR*pnmhdr, LRESULT *plr) 

  LPNMTOOLBAR pnmtb =reinterpret_cast<LPNMTOOLBAR>(pnmhdr);      
  // ... 
}   
2. error C2668: 'sqrt' : ambiguouscall to overloaded function
    原因:在VS2005中存在sqrt函数的重载。当编译器看到sqrt(int)时,找不到相应的函数,此时存在sqrt(float)和sqrt(long double)两个函数,编译器不知道程序员需要哪个函数,就会出现错误。

    解决办法:可以使用sqrtf( )代替。

3. error C2039: 'ReadHuge' : isnot a member of 'CFile

    原因:VS2005中,readhuge被read包括了。

4. error C2668: 'pow' : ambiguouscall to overloaded function

    在VS2005中,需要写成pow((double)i, 2),原因我没有查到。
    在VS2005中,函数原型为 double__cdecl pow(__in double _X, __in double _Y);

    在VC6中,函数原型为_CRTIMP double __cdecl pow (double, double);

5.ON_WM_NCHITTEST (and other MFCmacros) won't compile in VS2005。

    VS2005中,ON_WM_NCHITTEST宏编译不过:

    error C2440: 'static_cast' : cannot convertfrom 'UINT (__thiscall CMenuBar::* )(CPoint)' to 'LRESULT (__thiscall CWnd::*)(CPoint)' Cast from base to derived requires dynamic_cast or static_cast

    解决办法:修改OnNcHitTest的原型:

    VC6:afx_msg UINT OnNcHitTest(CPointpoint); ->

    VS2005:afx_msg LRESULT OnNcHitTest(CPointpoint);

6. error C2440: 'static_cast' :cannot convert from 'void (__thiscall CSettingStart::* )(BOOL,HANDLE)' to 'void(__thiscall CWnd::* )(BOOL,DWORD)'

    ON_WM_ACTIVATEAPP 的消息处理函数原型:

    VC6:afx_msg void OnActivateApp( BOOL,HANDLE);

    VS2005:afx_msg void OnActivateApp( BOOL,DWORD );


二.  其它问题

1. 以前可以这样用try

[cpp] 
catch(CException*e) 

    pApp->Warn("%s",e->GetErrorMessage); 
 
     e->Delete(); 
 
     return FALSE; 
 

    现在必须修改为:

[cpp] 
catch(CException*e) 

   TCHAR errormsg[255]; 
   e->GetErrorMessage(errormsg,255,NULL); 
   pApp->Warn("%s",errormsg); 
   e->Delete(); 
   return FALSE; 

2. strchr必须强制转换一下。
    VC6:char *str2=strchr(line,'|');

    VS2005: char *str2=(char *)strchr(line,'|');

3. VS2005中有些可能引起内存越界的函数不建议使用。

[cpp]
char c[10]; 
strcpy(c, "testtestts"); //ok with VC6, but not in VS2005 
strcpy_s(c, _countof(c),"testtestt");//9 chars, ok inVS2005 
strcpy_s(c, _countof(c),"testtestt");//10 chars,assert!!!!! in VS2005 
4. error LNK2019: unresolvedexternal symbol "wchar_t * __stdcall _com_util::Co.....
    解决方法:Property page ->C/C++ ->Language ->treatWchar-t   改为 No

5. 字符处理

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

6. 数学函数检查

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

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

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

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

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

    试图匹配参数列表“(int, int)”时,正确的使用为pow(2.0, 45)

7. 更加符合C++标准

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

    在VC6中:

[cpp] 
for(int i=0;i<100;i++) 

  f2(); 

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

    而有VS2005中,第二个循环中的i必须重新定义。

8. Create问题

    CDlg *dlg=new CDlg;

    dlg->create(IDD_DLG,this);//出错之处

    error C2660: 'Create' : function does nottake 2 parameters且我将第二个参数去掉的时候,又会显示

    error C2660: 'Create' : function does nottake 1 parameters19.error C2871: 'System' : a namespace with this name does notexist

    原因:这个错误只能说VC编译器还不够智能啊

    解决办法:在使用前需要使用Common Language Runtime Support (/clr).

    在配置属性中,选择general-》选择clc (ConfigurationProperties/General/Common Language Runtime support)

 

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,