首页 技术 正文
技术 2022年11月23日
0 收藏 858 点赞 3,393 浏览 2700 个字

最近。有一个项目开发,需要使用一次项目的代码。只有当项目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代码的时候也要考虑这些。代码的可移植性、可读性都是代码质量非常重要的方面。

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

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

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:7,947
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:4,822
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:5,651
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:5,517
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:6,718
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,188