• VC6迁移到VS2008几个问题——良好的代码,从我做起,从现在开始。


            最近。有一个项目开发,需要使用一次项目的代码。只有当项目VC6下编译通过的,在VS2008下不一定编译通过,能编译通过也不一定能链接成功。以下总结一下我在一个VC6项目移植到VS2008中遇到的一些问题以及解决的方法。

     

    1 类型转换

    1WCHAR *wszFind = wcschr( wszText,(WCHAR)'@');

    这个代码在VC6下编译是没问题的。但在VS2008下。编译会报错,就是类型转换的问题,改动例如以下:

    WCHAR *wszFind = (WCHAR *)wcschr( wszText,(WCHAR)'@');

     

     

    2

    1)

    int ConvertAnsiToUnicode16(const CHAR * szIn,CHARU16 *szOut,int nSize )

    以下在VS2008编译不通过。

    nLen = ConvertAnsiToUnicode( szIn,szOut,nSize );

    例如以下改动能够:

    nLen = ConvertAnsiToUnicode( szIn, (WCHAR *)szOut,nSize );

     

    就是说VS2008,对代码的要求更严格,会对类型做检測。不会支持默认的类型强制转换。

     

    2 DWORD dwPower = (DWORD)(pow(2.0f,(double)m_dwArraySize ));

    编译提演示样例如以下:

    XXX.cpp(58) : error C2666:pow: 6个重载有相似的转换

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(575):可能是“long double pow(long double,int)

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(573):       long double pow(long double,long double)

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(527):       float pow(float,int)

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(525):       float pow(float,float)

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(489):       double pow(double,int)

    C:Program FilesMicrosoft Visual Studio 9.0VCincludemath.h(123):       double pow(double,double)

     

    这个就是重载函数的问题。

    能够例如以下:

    方法一:

    UINT nRes = (UINT)pow( (double)10.0f,(double)(m_nListCount-1) )*nMinRes;

    方法二:

    Double dValue = 10.0f

    UINT nRes = (UINT)pow(dValue ,(double)(m_nListCount-1) )*nMinRes;

     

    3 const BUFLEN = 26*4;

    VS2008编译提示:

    error C4430:缺少类型说明符 - 假定为 int。注意: C++不支持默认 int

    改动例如以下:

    const int BUFLEN = 26*4;

     

    4 作用域

           for (int i = n; i < n + 16; i++)

           {

                  m_bySeed[i % sizeof(m_bySeed)] ^= by16[i];

           }

           m_nUpdateCounter++;

           if (0 == (m_nUpdateCounter % 1024))

           {

                  SaveSeedIntoFile();

           }

           for (i = 0; i < sizeof(m_bySeed); i++)

           {

                  m_bySeed[i] ^= rand() % 0xff;

           }

    上面代码在VS2008,会提示i未定义,这个就会作用域的问题。

    改动例如以下:

    int i;

     

           for (i = n; i < n + 16; i++)

           {

                  m_bySeed[i % sizeof(m_bySeed)] ^= by16[i];

           }

           m_nUpdateCounter++;

           if (0 == (m_nUpdateCounter % 1024))

           {

                  SaveSeedIntoFile();

           }

           for (i = 0; i < sizeof(m_bySeed); i++)

           {

                  m_bySeed[i] ^= rand() % 0xff;

           }

     

    5 宏定义

    typedef enum _STORAGE_QUERY_TYPE {

           PropertyStandardQuery = 0,          // Retrieves the descriptor

           PropertyExistsQuery,                // Used to test whether the descriptor is supported

           PropertyMaskQuery,                  // Used to retrieve a mask of writeable fields in the descriptor

           PropertyQueryMaxDefined     // use to validate the value

    } STORAGE_QUERY_TYPE, *PSTORAGE_QUERY_TYPE;

     

    VS2008下编译提示与系统反复,所以在宏定义的时候,一定要添加与自己project功能相关的keyword,用来差别。

     

    6 抛异常

    例如以下

           if(keysize<1)

                  throw exception("Incorrect key length");

    这样的代码在VC6下能够编译,在VS2008编译只是,解决的方法直接屏蔽。

     

    总结:

        上面提到的问题。事实上大部分是一个编写习惯的问题。

    在写server代码,一定要考虑跨平台编译的问题,vc6VS2008linux等。各种平台的编译器对代码的检查要求不一样,可是仅仅要遵守C++编写规范,这些问题都能够避免。而在写client代码的时候也要考虑这些。代码的可移植性、可读性都是代码质量非常重要的方面。

    程序猿都不喜欢看别人的代码,改别人的代码,认为难看、难懂、难理解。可是自己在写代码的时候。又在给别人问候自己娘的机会。

        好的代码。从我做起,从如今做起。

     

     转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/26007423

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    在Excel中查找/替换时使用换行符
    用fieldset标签轻松实现Tab选项卡效果
    用JSFL将Flash中的元件导出为PNG
    PHP学习笔记之PDO
    网页中的数学公式
    解决fl.findObjectInDocByType/fl.findObjectInDocByName的毛病
    HTML+CSS 网页中鼠标滚轮失效的解决办法
    jQuery 离开页面时提示
    ASP 计算出指定年份生肖
    au3创建Access数据库表例子
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4685070.html
Copyright © 2020-2023  润新知