在2005年年中的时候,公司就准备转移到Visual Studio 2005上开发产品。本人有幸参与了升级的过程,成功的把30个左右solutions、几百个projects升级到了VC8。由于项目众多,并且项目还在持续开发中,我们的基本策略是让代码同时在VC7.1和VC8下面编译通过,最后再完全转到VC8,链接并运行成功。从总体上说VC8比VC7.1语法上严格且严谨。下面是我的一些经验总结。文中只涉及到编译问题,链接的问题没有包含在内。我的同事Mr Han对此文有巨大贡献,在此非常感谢。
1. 变量作用域
在vc7.1中, 如果一个变量定义在for语句的条件从句中,那么这个变量可以在for之后使用。但Vc8禁止这样,会报告一个C2065错误.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
在for语句之前声明变量(可保证代码在vc7.1和vc8下同时编译通过)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2. 指针和引用的声明
在Vc7.1中, 下面的代码可以编译, 但是vc8会报C4430 错误。(很难想象有些美国程序员竟然这样声明)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
把* 或&放到类型的后面.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
3. 默认int类型
在vc7.1中,如果定义一个变量但不声明类型,那么默认为int。VC8不支持。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
加上int.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
4. 函数的默认返回值类型
同上,VC8不支持把 int 作为默认返回值类
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
解决方法:
明确声明函数返回值类型为 int.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
5. 函数地址
Vc7中函数名就是地址。在vc8中,必须要使用&操作符同时写出这个方法的全名(fully qualified name).
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法:
加上 &.
![](/Images/OutliningIndicators/None.gif)
6. 隐式类型转换
VC8不允许B* 到const B*&的隐式转换.
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法:
强制转换或函数参数变成const B*。
![](/Images/OutliningIndicators/None.gif)
7. 友元方法(Friend function)
VC8不允许声明一个private或protected函数为友元.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法 1:
声明友元类.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
解决方法 2:
把函数声明为public
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
8. STL的stdext 命名空间
在vc8中,hash_map 和hash_set 被移进了stdext命名空间中.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
使用stdext 命名空间.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
9. 头文件
许多头文件如fstream.h 和iostream.h在VC8中已经不存在了.
![](/Images/OutliningIndicators/None.gif)
解决方法:
使用STL.
![](/Images/OutliningIndicators/None.gif)
10. Iterator
一些 STL 类, iterators 不再用指针实现
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
![](/Images/OutliningIndicators/None.gif)
11. Enum
使用一个Enum的成员时,不要使用enum的名字
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
解决方法:
去掉Enum 的名字.
![](/Images/OutliningIndicators/None.gif)