博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
升级VC7项目到VC8的注意事项
阅读量:7257 次
发布时间:2019-06-29

本文共 2290 字,大约阅读时间需要 7 分钟。

1. 变量作用域 

在vc7.1中, 如果一个变量定义在for语句的条件从句中,那么这个变量可以在for之后使用。但Vc8禁止这样,会报告一个C2065错误.

None.gif 
for  ( 
int  i  =   0 ; i  <   10 ;  ++ i)  
ExpandedBlockStart.gif  {                             
InBlock.gif   
dot.gif 
// 
codes here 
ExpandedBlockEnd.gif 
None.gif 
if  (i  <   10 )..  
// 
error in Vc8 
None.gif 
for  (i  =   0 ; i  <   5 ;  ++ i)  
// 
error in Vc8

解决方法:

在for语句之前声明变量(可保证代码在vc7.1和vc8下同时编译通过)

None.gif 
int  i  = 0 ;                    
None.gif 
for  (i  =   0 ; i  <   10 ;  ++ i)
None.gif 
for  (i  =   0 ; i  <   5 ;  ++ i)

2. 指针和引用的声明 

在Vc7.1中, 下面的代码可以编译, 但是vc8会报C4430 错误。(很难想象有些美国程序员竟然这样声明)

None.gif 
const   &   
int  a;  
// 
error in VC8 
None.gif 
const   *   
int  b;   
// 
error in VC8 
None.gif 
int  myfun ( 
const   &  B);  
// 
error in VC8

解决方法:

把* 或&放到类型的后面.

None.gif 
const   
int &  a; 
None.gif 
const   
int *  b;
None.gif 
int  myfun ( 
const  B & );

3. 默认int类型 

在vc7.1中,如果定义一个变量但不声明类型,那么默认为int。VC8不支持。

None.gif 
static  i  =   0 ;  
// 
 C4430 error in Vc8  
None.gif 
const  i  =   0 ;  
// 
C4430 error

解决方法:

加上int.

None.gif 
static   
int  i  =   0 ; 
None.gif 
const   
int  i  =   0 ;

4. 函数的默认返回值类型 

同上,VC8不支持把 int 作为默认返回值类

None.gif Func()
ExpandedBlockStart.gif  { 
return   0 ;} ;  
// 
error in VC8

解决方法:

明确声明函数返回值类型为 int.

None.gif 
int  Func()
ExpandedBlockStart.gif  { 
return   0 ;} ;

5. 函数地址 

Vc7中函数名就是地址。在vc8中,必须要使用&操作符同时写出这个方法的全名(fully qualified name).

None.gif 
class  A
ExpandedBlockStart.gif  {
InBlock.gif 
public :
InBlock.gif      
int  Test( 
void );
ExpandedBlockEnd.gif} ;
None.gif 
void  fun( 
int  (A:: * test) ( 
void ));
None.gif 
int  main() 
ExpandedBlockStart.gif  {
InBlock.gif     fun(A::Test); 
// 
C3867 error in VC 
InBlock.gif 
      
return   0 ;
ExpandedBlockEnd.gif}

解决方法:

加上 &.

None.gif fun( & A::Test);

6. 隐式类型转换

VC8不允许B* 到const B*&的隐式转换.

ExpandedBlockStart.gif 
class  B  {} ;
None.gif 
void  fun (  
const  B *   &  ); 
// 
if possible use const B* instead 
None.gif 
int  main() 
ExpandedBlockStart.gif  {
InBlock.gifB  * test  =   
new  B();
InBlock.giffun (test);  
// 
error in VC8 
InBlock.gif 
return   0 ;
ExpandedBlockEnd.gif}

解决方法:

强制转换或函数参数变成const B*。

None.gif 
void  fun (  
const  B *   );

7. 友元方法(Friend function) 

VC8不允许声明一个private或protected函数为友元.

None.gif 
class  A
ExpandedBlockStart.gif  {
InBlock.gif 
private :
InBlock.gif  
void  c();  
ExpandedBlockEnd.gif} ;
None.gif 
class  B
ExpandedBlockStart.gif  {
InBlock.gif  friend  
void  A::c();  
// 
C2248 error, c() is invisible to class B. 
ExpandedBlockEnd.gif 
} ;

解决方法 1:

声明友元类.

None.gif 
class  A
ExpandedBlockStart.gif  {
InBlock.gif 
private :
InBlock.gif  
void  c();  
ExpandedBlockEnd.gif} ;
None.gif 
class  B
ExpandedBlockStart.gif  {
InBlock.gif  friend  
class  A;
ExpandedBlockEnd.gif} ;

解决方法 2:

把函数声明为public

None.gif 
class  A
ExpandedBlockStart.gif  {
InBlock.gif 
public :
InBlock.gif  
void  c();  
ExpandedBlockEnd.gif} ;
None.gif 
class  B
ExpandedBlockStart.gif  {
InBlock.gif  friend  
void  A::c();
ExpandedBlockEnd.gif} ;

8. STL的stdext 命名空间 

在vc8中,hash_map 和hash_set 被移进了stdext命名空间中.

None.gif #include  < hash_map > 
None.gifstd::hash_map  
// 
error in VC8

解决方法:

使用stdext 命名空间.

None.gif #include  < hash_map > 
None.gifstdext::hash_map

9. 头文件 

许多头文件如fstream.h 和iostream.h在VC8中已经不存在了.

None.gif #include  < fstream.h >   
// 
error in VC8

解决方法:

使用STL.

None.gif #include  < fstream >

10. Iterator 

一些 STL 类, iterators 不再用指针实现

None.gif std::vector < DMDetailRow >  m_data;
None.gifstd::vector < DMDetailRow > ::iterator iter  =   & m_data[rowNum];

解决方法:

None.gif std::vector < DMDetailRow > ::iterator Iter  =  m_data.begin()  +  rowNum;

11. Enum 

使用一个Enum的成员时,不要使用enum的名字

None.gif 
enum  E
{
InBlock.gif  a,b,c
ExpandedBlockEnd.gif} ;
None.gifE e1  =  E::a;  
// 
warning in VC8

解决方法:

去掉Enum 的名字.

None.gif E e1  =  a;
你可能感兴趣的文章