以下程序显示错误:
#include<conio.h>
#include<iostream.h>
int count = 0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n Number of objects created "<<count;
}
~alpha()
{
cout<<"\n Number of object destroyed"<<count;
count--;
}
};
int main
{
cout<<" inside main ";
alpha a1, a2, a3, a4;
{
cout<<"\n Block 1 ";
alpha A5;
}
{
cout<<"\n Block 2 ";
alpha a6;
}
cout<<" main again ";
return 0;
}
Line 11: error: reference to 'count' is ambiguous compilation terminated due to -Wfatal-errors.
最佳答案
没有标题<iostream.h>
在标准 C++ 中。使用标题 <iostream>
在 std
中有名字命名空间,因此不会污染名称为 count
的全局命名空间.
不要忘记使用 std::cin
, std::cout
等等。
如果你的编译器不能识别<iostream>
,把它扔掉,换一个新的。 Visual Studio Express一方面,免费且易于使用,虽然目前不是非常符合标准,但这对您来说应该不是什么大问题。
关于c++ - 为什么这个使用构造函数和析构函数的 C++ 程序显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7309302/