您的位置:首页 > 编程语言

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

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

1
类型转换

1)WCHAR *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下编译提示与系统反复,所以在宏定义的时候,一定要添加与自己project功能相关的keyword,用来差别。

6
抛异常

例如以下

if(keysize<1)

throw exception("Incorrect key length");

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

总结:

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

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

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

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

转载请注明原创链接:/article/1627964.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: