VC7转换到VC8的常见问题
从总体上说VC8比VC7.1语法上严格且严谨。文中只涉及到编译问题,链接的问题没有包含在内。
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
int i =0;
for (i = 0; i < 10; ++i)
for (i = 0; i < 5; ++i)
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&);
static i = 0; // C4430 error in Vc8
const i = 0; //C4430 error
static int i = 0;
const int i = 0;
Func()
{return 0;}; //error in VC8
int Func()
{return 0;};
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);
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;
}
void fun ( const B* );
class A
{
private:
void c();
};
class B
{
friend void A::c(); //C2248 error, c() is invisible to class B.
};
class A
{
private:
void c();
};
class B
{
friend class A;
};
class A
{
public:
void c();
};
class B
{
friend void A::c();
};
#include <hash_map>
std::hash_map //error in VC8
#include <hash_map>
stdext::hash_map
#include <fstream.h> //error in VC8
#include <fstream>
std::vector<DMDetailRow> m_data;
std::vector<DMDetailRow>::iterator iter = &m_data[rowNum];
std::vector<DMDetailRow>::iterator Iter = m_data.begin() + rowNum;
enum E
{
a,b,c
};
E e1 = E::a; //warning in VC8
E e1 = a;
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)