c++ - 为什么这个使用构造函数和析构函数的 C++ 程序显示错误?

标签 c++ constructor destructor

以下程序显示错误:

#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/

相关文章:

c++ - 从文件中读取 CSV 数据

c++ - 分段故障(核心已转储)-二维数组

c++ - 如何在 C++ 中初始化嵌套类的构造函数

c++ - 模板化手动析构函数调用不起作用

c++ - 为什么对象 'destructed' 两次?

c++ - 返回值会被临时销毁吗?

android - Android NDK 中的 std::thread 被错误地卡住了

c++ - 大小 8 的无效读取,大小 8 的无效写入 (Valgrind)

python - 使用默认参数提升 python make_constructor

c++ - 为什么我们不能将 char 分配给字符串?