简体   繁体   English

C++ 命名空间错误:不是命名空间的成员

[英]C++ Namespace error : not a member of namespace

I've declared a variable in a namespace as extern in a header file, say ah I'm including this file in a .cpp file say a.cpp and defining that variable in the same namespace.我已经在头文件中将命名空间中的变量声明为 extern,比如说啊,我将这个文件包含在一个 .cpp 文件中,比如 a.cpp 并在同一个命名空间中定义该变量。 This works fine.这工作正常。

Now, I'm including this header file in another .cpp file, say b.cpp and using this variable in a different namespace.现在,我将这个头文件包含在另一个 .cpp 文件中,比如 b.cpp 并在不同的命名空间中使用这个变量。 This throws me the error saying variable 'a' is not declared in this scope.这向我抛出错误,指出变量“a”未在此范围内声明。 I include the namespace using 'using namespace' in b.cpp file.我在 b.cpp 文件中使用“使用命名空间”包含命名空间。 Still I get the same error.我仍然得到同样的错误。 Using the expression namespace::variable in b.cpp gives the error saying variable 'a' is not a member of that namespace.在 b.cpp 中使用表达式 namespace::variable 会出现错误,指出变量 'a' 不是该命名空间的成员。

Following is the code snippets for understanding my problem.以下是用于理解我的问题的代码片段。

// a.h
namespace example1
{ 
extern int a; 
}
// a.cpp
#include"a.h"
namespace example1
{ 
a = 10; 
}
// b.cpp
#include"a.h"
namespace example2
{ 
a = 5; // error : 'a' not declared in this scope. 
}
// b.cpp
#include"a.h"
using namespace example1;
namespace example2
{ 
a = 5; // error : 'a' not declared in this scope. 
}
// b.cpp
#include"a.h"
namespace example2
{ 
example1::a = 5; // error : 'a' not a member of example1. 
}

Inside ah you declare a variable as extern, so the definition will have to appear in a cpp file (translation unit to be correct):在 ah 中,您将变量声明为 extern,因此定义必须出现在 cpp 文件中(翻译单元是正确的):

 // a.h
 namespace example1
 { 
     extern int a; 
 }

So here is the file where you define the variable:所以这里是你定义变量的文件:

 // a.cpp
 #include"a.h"
 namespace example1
 { 
    int a = 10; 
 }

Now you use the variable a defined in a.cpp现在您使用 a.cpp 中定义的变量a

// b.cpp
#include"a.h"
namespace example2
{ 
   void f(){
    a = 5; // error : 'a' not declared in this scope. 
   }
}

And indeed, a is in namespace example1, a is not qualified name and compiler fails.事实上,a 在命名空间 example1 中,a 不是限定名称并且编译器失败。 (Compiler look for a in namespace ::example2 and its parent the global namespace ) (编译器在命名空间 ::example2 及其父级中查找全局命名空间)

Second example:第二个例子:

 // b.cpp
 #include"a.h"
 using namespace example1;
 namespace example2
 { 
   void f(){
     a = 5; 
   }
 }

This should compile because now a is visible at the global namespace scope.这应该可以编译,因为现在a在全局命名空间范围内可见。 But if you still see an error now, it shall be at the linkage phase.但是,如果您现在仍然看到错误,则应该是在链接阶段。 Before building the executable, the linker is going to look for a definition of a so you must also give him the result of compiling a.cpp.构建可执行文件之前,链接器将要寻找的定义a ,所以你也必须给他编译a.cpp的结果。 For example if you use GCC, compile with this command line c++ b.cpp a.cpp .例如,如果您使用 GCC,请使用此命令行c++ b.cpp a.cpp

This also applies to this example:这也适用于这个例子:

  // b.cpp
 #include"a.h"
 namespace example2
 { 
    void f(){
       example1::a = 5; 
    }
 }

Here you perform qualified name look up, so it should compile.在这里您执行限定名称查找,因此它应该编译。 If error occurs this should be at link time as described in the previous example.如果发生错误,这应该是在前一个示例中描述的链接时。

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

相关问题 Xcode中的C ++-命名空间中没有成员 - C++ in Xcode - no member in namespace C++:错误:全局命名空间中没有名为“signbit”的成员 - C++: error: no member named 'signbit' in the global namespace c++ 编译失败并出现错误:命名空间“std”中没有名为“snprintf”的成员 - c++ compile fail with error: no member named 'snprintf' in namespace 'std' C ++-不是类或名称空间错误 - C++ - is not a class or namespace error 使用C ++和命名空间编译错误 - Compiling Error with C++ and namespace 带有 libpqxx-header 的 C++ 产生错误 C2039:“encoding_group”:不是“全局命名空间”的成员 - C++ with libpqxx-header produces error C2039: 'encoding_group': is not a member of '`global namespace'' C ++:extern“C”和类成员之间的命名空间冲突 - C++: namespace conflict between extern “C” and class member 卡特琳娜 C++:使用<cmath>标头产生错误:全局命名空间中没有名为“signbit”的成员</cmath> - Catalina C++: Using <cmath> headers yield error: no member named 'signbit' in the global namespace VS Code C++ 扩展给出错误“命名空间没有成员函数” - VS Code C++ extension gives error “namespace has no member function” C ++ begineer:使用命名空间错误 - C++ begineer : using namespace error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM