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.
Solution:
Declare the variable before the FOR statement
2. Variable declaration
In Vc7.1, these codes can compile, but VC8 compiler reports a C4430 error
Solution:
Put * or & after type.
3. Default int
VC8 doesn’t support Default int
Solution:
Add int.
4. Function's return value type
VC8 doesn’t treat int as default return value type
Solution:
Add int.
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.
Solution:
Add &.
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
7. Friend function
VC8 doesn’t allow declaring a private or protected function as a friend function.
Solution 1:
Declare friend class.
Solution 2:
Make the function public
8. stdext namespace
hash_map and hash_set are moved into stdext namespace in VC8.
std::hash_map //error in VC8
Solution:
Use stdext namespace.
9. Header files
Many headers such as fstream.h and iostream.h are removed in VC8.
Solution:
Use STL headers.
10. Iterator
In some Standard C++ Library classes, iterators are no longer implemented by pointers.
Solution:
11. Enum
Don't qualify the member of an Enum with the Enum name
{
a,b,c
};
E e1 = E::a; //warning in VC8
Solution:
Remove Enum name.