简体   繁体   English

C ++中的名称空间有效性

[英]namespace validity in c++

I have a simple question for namespace in C++. 我对C ++中的名称空间有一个简单的问题。 There are errors when I compiling the following little piece of code. 当我编译以下几段代码时出现错误。 I don't understand why. 我不明白为什么。 Thanks for any help in advance! 感谢您的任何帮助!

#include <iostream>
using namespace std;

int x=100;

namespace first
{
     int x=1;
}
namespace second
{
     int x=2;
}

int main(){

     {
        using namespace first;
        cout<<x<<endl;
     }

     {
        using namespace second;
        cout<<x<<endl;
     }

     cout<<x<<endl;
}

If I comment out the x declared in the global scope and the last statement. 如果我注释掉在全局范围内声明的x和最后一个语句。 It works fine. 工作正常。 But in my mind, the first x is declared in the std namespace and using namespace first and second in the main will be invalid after the the code block they are declared(so the namespace will be std again). 但是在我看来,第一个x是在std命名空间中声明的,并且在声明了代码块后,在main中使用名称空间的first和second将无效(因此,命名空间将再次被std)。 So the above code should work. 因此,上面的代码应该可以工作。 Where am I wrong? 我哪里错了?

But in my mind, the first x is declared in the std namespace 但在我看来,第一个x是在std命名空间中声明的

Your mind is wrong. 你的想法错了。 The using namespace std makes names from std available in the global namespace, it doesn't mean names declared in the global namespace are in std . using namespace std使std中的名称在全局名称空间中可用,并不意味着在全局名称空间中声明的名称在std

x is declared in the global namespace. x在全局名称空间中声明。

so the namespace will be std again 所以命名空间将再次被std

No, nothing in your file is in namespace std , only the contents of <iostream> are in namespace std . 不,文件中没有任何内容位于名称空间std ,只有<iostream>的内容位于名称空间std

The error is that when you try to use x there are two different variables in scope, ::x and first::x that you could be referring to, so it is ambiguous. 错误是,当您尝试使用x ,您可能要引用的作用域中有两个不同的变量::xfirst::x ,所以它是模棱两可的。 You can disambiguate with a using declaration instead of a using directive: 您可以使用using声明而不是using指令来消除歧义:

 {
    using first::x;
    cout<<x<<endl;
 }

This says that in that scope x refers to first::x 这表示在该范围内x指的是first::x

in this case x variable is ambiguous. 在这种情况下,x变量是不明确的。 The compiler can't find which x you are going to use. 编译器找不到要使用的x。 You can write like this. 您可以这样写。

#include <iostream>
using namespace std;

int x=100;

namespace first
{
     int x=1;
}
namespace second
{
     int x=2;
}

int main(){

     {
        cout<<first::x<<endl;
     }

     {
        cout<<second::x<<endl;
     }

     cout<<x<<endl;
}

now your code will compile. 现在您的代码将被编译。

The first x is not in the std namespace. 第一个x不在std名称空间中。 It would only be in the std namespace if defined like this: 如果这样定义,它将仅位于std命名空间中:

namespace std
{
    int x;
}

Because it's not in that namespace, it's matched by references to x later in the program, and such references become ambiguous when another namespace declaring x is also being used (as in using namespace first / second). 因为它不在该命名空间中,所以在程序中稍后会与对x的引用匹配,并且当另一个声明x的命名空间也正在使用时,此类引用就变得模棱两可(例如在第一/第二个命名空间中使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM