• 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

  • 相关阅读:
    Python的time模块随笔。
    生成器递归解决八皇后问题(勉强理解)
    Python历史「解密」Python底层逻辑 及Python 字节码介绍(转帖)
    可迭代(Interable),迭代器(Iterator),生成器(generator)的手记(11月26日再次修改)
    __getattr__,__setattr__,__delattr__,__getattribute__,记录
    关于property的一些记录,以及描述符(descriptor)中__get__,__set__,__delete__的属性使用。
    Python魔法方法之容器部方法(__len__,__getitem__,__setitem__,__delitem__,__missing__)(更新版本)
    Mac下PyCharm快捷键大全!(每天记住几个)
    开笔了,就写一下,hasattr,getattr,setattr。
    GUI
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4062774.html
Copyright © 2020-2023  润新知