In the mid-year of 2005, my company started to port our product from vc7.1 to vc8. Our strategy is to make source code compile both in VC7.1 and in VC8. I was happy to be involved into porting. We ported almost 30 solutions and 200 projects. VC8 compiler is stricter than VC7.1. The followings are some tips. (Linking problems are not included.) My colleague Mr. Han made great contribution to this article. Many thanks to Han.

1. Variable scope
In vc7.1, if a variable is declared in a FOR loop, it can be used after this FOR loop. But Vc8 compiler will report a C2065 error.

Tips of porting VC7 projects to VC8for (int i = 0; i < 10++i)  
error in Vc8

Solution:
Declare the variable before the FOR statement

Tips of porting VC7 projects to VC8int i =0;                    
Tips of porting VC7 projects to VC8
for (i = 0; i < 10++i)
Tips of porting VC7 projects to VC8
for (i = 0; i < 5++i)


 
2. Variable declaration
In Vc7.1, these codes can compile, but VC8 compiler reports a C4430 error

Tips of porting VC7 projects to VC8const & int a; //error in VC8
Tips of porting VC7 projects to VC8
const * int b;  //error in VC8
Tips of porting VC7 projects to VC8
int myfun (const & B); //error in VC8


Solution:
Put * or & after type.

Tips of porting VC7 projects to VC8const int& a; 
Tips of porting VC7 projects to VC8
const int* b;
Tips of porting VC7 projects to VC8
int myfun (const B&);


 
3. Default int
VC8 doesn’t support Default int

Tips of porting VC7 projects to VC8static i = 0// C4430 error in Vc8 
Tips of porting VC7 projects to VC8
const i = 0//C4430 error


Solution:
Add int.

Tips of porting VC7 projects to VC8static int i = 0
Tips of porting VC7 projects to VC8
const int i = 0;


 
4. Function's return value type
VC8 doesn’t treat int as default return value type

Tips of porting VC7 projects to VC8Func()
error in VC8


Solution:
Add int.

Tips of porting VC7 projects to VC8int Func()
;


 
5. Function address
To create a pointer to a member function, the address of operator (&) must be used with the fully qualified name of the method. Not having to use the & operator and the fully qualified name of the method lead to logic bugs in code due missing parenthesis in function calls.

Tips of porting VC7 projects to VC8class A
}


Solution:
Add &.

Tips of porting VC7 projects to VC8fun(&A::Test);


 
6. Reference to a pointer to a const variable
VC8 doesn’t allow implicit conversion from B* to const B*&.

}


Solution:
c-style cast. If possible, use const B* instead

Tips of porting VC7 projects to VC8void fun ( const B*  );

 
7. Friend function
VC8 doesn’t allow declaring a private or protected function as a friend function.

Tips of porting VC7 projects to VC8class A
;


Solution 1:
Declare friend class.

Tips of porting VC7 projects to VC8class A
;


Solution 2:
Make the function public

Tips of porting VC7 projects to VC8class A
;


 
8. stdext namespace
hash_map and hash_set are moved into stdext namespace in VC8.

#include <hash_map>
std::hash_map 
//error in VC8


Solution:
Use stdext namespace.

Tips of porting VC7 projects to VC8#include <hash_map>
Tips of porting VC7 projects to VC8stdext::hash_map


 
9. Header files
Many headers such as fstream.h and iostream.h are removed in VC8.

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

Solution:
Use STL headers.

#include <fstream>


 
10. Iterator
In some Standard C++ Library classes, iterators are no longer implemented by pointers.

Tips of porting VC7 projects to VC8std::vector<DMDetailRow> m_data;
Tips of porting VC7 projects to VC8std::vector
<DMDetailRow>::iterator iter = &m_data[rowNum];


Solution:

Tips of porting VC7 projects to VC8std::vector<DMDetailRow>::iterator Iter = m_data.begin() + rowNum;

 
11. Enum
Don't qualify the member of an Enum with the Enum name

enum E
{
  a,b,c
};
E e1 
= E::a; //warning in VC8


Solution:
Remove Enum name.

Tips of porting VC7 projects to VC8E e1 = a;

 

相关文章: