VC6项目移植到VS2008的若干有关问题——好的代码,从小弟我做起,从现在做起

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 Files\Microsoft Visual Studio 9.0\VC\include\math.h(575):可能是“long double pow(long double,int)

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\math.h(573):       long double pow(long double,long double)

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\math.h(527):       float pow(float,int)

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\math.h(525):       float pow(float,float)

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\math.h(489):       double pow(double,int)

C:\Program Files\Microsoft Visual Studio 9.0\VC\include\math.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下编译提示与系统重复,所以在宏定义的时候,一定要增加与自己工程功能相关的关键字,用来区别。

 

6 抛异常

如下

       if(keysize<1)

              throw exception("Incorrect key length");

这种代码在VC6下可以编译,在VS2008编译不过,解决办法直接屏蔽。

 

总结:

    上面提到的问题,其实大部分是一个编写习惯的问题。在写服务器代码,一定要考虑跨平台编译的问题,vc6VS2008linux等。各种平台的编译器对代码的检查要求不一样,但是只要遵守C++编写规范,这些问题都可以避免。而在写客户端代码的时候也要考虑这些,代码的可移植性、可读性都是代码质量很重要的方面。程序员都不喜欢看别人的代码,改别人的代码,觉得难看、难懂、难理解。但是自己在写代码的时候,又在给别人问候自己娘的机会。

    好的代码,从我做起,从现在做起。

 

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