小弟是C++新生,近日看到有关“命名空间”的资料,被解释为将程序库名称封装起来的方法,可以避免和应用程序发生命名冲突的问题。我按它给的例程创建了一个Win32 Console Application运行很顺利,但当把“using namespace std;”这一句去掉,程序就编译不过去了,但我在一般的MFC AppWizard(exe)创建的程序中并没有找到“using namespace std;”这一句,而且程序运行平稳,为什么会这样?不知哪位“大虾”能指教一二。

解决方案 »

  1.   

    如果你用了#inlcude <*.h>就不需要用using namespace std;
    如果你用了#inlcude <*>就要用。
    比如#include <iostream>
    using namespace std;
      

  2.   

    试过了,还是不行,源程序如下:
    #include<iostream.h>
    #include<string.h>
    //using namespace std;
    int main()
    {
    string user_name;
    cout<<"Please enter your first name:";
    cin>>user_name;
    cout<<'\n'
        <<"Hello,"
        <<user_name
                 <<"...and goodbye!\n";
    return 0;
    }
      

  3.   

    看看有关Koenig Lookup的内容就知道了,见《Exceptional C++》。
      

  4.   

    这么写:
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
       string user_name;
       cout << "Please enter your first name:";
       cin >> user_name;
       cout << '\n' <<"Hello," <<user_name <<"...and goodbye!\n";
       return 0;
    }或者这么写:
    #include<iostream>
    #include<string>int main()
    {
       std::string user_name;
       std::cout << "Please enter your first name:";
       std::cin >> user_name;
       std::cout << '\n' <<"Hello," <<user_name <<"...and goodbye!\n";
       return 0;
    }
    都可以编译通过。
      

  5.   

    up一下!
    这个程序是在Essential c++里的,我也看不懂呀,基础太差,唉~~~~