(来源不详) 1. 变量作用域 在vc7.1中, 如果一个变量定义在for语句的条件从句中,那么这个变量可以在for之后使用。但Vc8禁止这样,会报告一个C2065错误. for ( int i = 0 ; i < 10 ; ++ i) { // codes here } if (i < 10 ).. // error in Vc8 for (i = 0 ; i < 5 ; ++ i) // error in Vc8 解决方法: 在for语句之前声明变量(可保证代码在vc7.1和vc8下同时编译通过) int i = 0 ; for (i = 0 ; i < 10 ; ++ i) for (i = 0 ; i < 5 ; ++ i) 2. 指针和引用的声明 在Vc7.1中, 下面的代码可以编译, 但是vc8会报C4430 错误。(很难想象有些美国程序员竟然这样声明) const & int a; // error in VC8 const * int b; // error in VC8 int myfun ( const & B); // error in VC8 解决方法: 把* 或&放到类型的后面. const int & a; const int * b; int myfun ( const B & ); 3. 默认int类型 在vc7.1中,如果定义一个变量但不声明类型,那么默认为int。VC8不支持。 static i = 0 ; // C4430 error in Vc8 const i = 0 ; // C4430 error 解决方法: 加上int. static int i = 0 ; const int i = 0 ; 4. 函数的默认返回值类型 同上,VC8不支持把 int 作为默认返回值类 Func() { return 0 ;} ; // error in VC8 解决方法: 明确声明函数返回值类型为 int. int Func() { return 0 ;} ; 5. 函数地址 Vc7中函数名就是地址。在vc8中,必须要使用&操作符同时写出这个方法的全名(fully qualified name). class A { public : int Test( void ); } ; void fun( int (A:: * test) ( void )); int main() { fun(A::Test); // C3867 error in VC return 0 ; } 解决方法: 加上 &. fun( & A::Test); 6. 隐式类型转换 VC8不允许B* 到const B*&的隐式转换. class B {} ; void fun ( const B * & ); // if possible use const B* instead int main() { B * test = new B(); fun (test); // error in VC8 return 0 ; } 解决方法: 强制转换或函数参数变成const B*。 void fun ( const B * ); 7. 友元方法(Friend function) VC8不允许声明一个private或protected函数为友元. class A { private : void c(); } ; class B { friend void A::c(); // C2248 error, c() is invisible to class B. } ; 解决方法 1: 声明友元类. class A { private : void c(); } ; class B { friend class A; } ; 解决方法 2: 把函数声明为public class A { public : void c(); } ; class B { friend void A::c(); } ; 8. STL的stdext 命名空间 在vc8中,hash_map 和hash_set 被移进了stdext命名空间中. #include < hash_map > std::hash_map // error in VC8 解决方法: 使用stdext 命名空间. #include < hash_map > stdext::hash_map 9. 头文件 许多头文件如fstream.h 和iostream.h在VC8中已经不存在了. #include < fstream.h > // error in VC8 解决方法: 使用STL. #include < fstream > 10. Iterator 一些 STL 类, iterators 不再用指针实现 std::vector < DMDetailRow > m_data; std::vector < DMDetailRow > ::iterator iter = & m_data[rowNum]; 解决方法: std::vector < DMDetailRow > ::iterator Iter = m_data.begin() + rowNum; 11. Enum 使用一个Enum的成员时,不要使用enum的名字 enum E { a,b,c } ; E e1 = E::a; // warning in VC8 解决方法: 去掉Enum 的名字. E e1 = a;