引用全局命名空间中的某个内容
referencing something from global namespace?
这确实很琐碎,但我遇到了一个意想不到的错误。
我有一些代码在名称空间中
以下是表示我的代码结构的一些伪代码:
namespace A {
void init() {
initialize_kitchen_sink();
}
#include "operations.h" // declares shake_and_bake()
void foo() {
shake_and_bake();
}
void cleanup() {
// do nothin' cuz i'm a slob
}
}
错误:
undefined reference to `A::shake_and_bake`
事实证明,将#include
移动到命名空间之外会修复它。
实际上,include将在A
命名空间内声明operations.h
中的所有函数。然后,它将徒劳地搜索实现。
我想,与其删除我的整个帖子,我还不如把它留到那一分钟,让其他人偶然发现类似的问题并获得启发。
为了准确地回答您的问题,您可以使用::作为您的第一个语句来引用全局名称空间中的某些内容,如:
void foo() {
::shake_and_bake();
}
当然,对于这种特殊情况,你的答案是正确的。
相关文章:
- 在命名空间中使用全局命名空间中的函数
- 将好友从模板导出到全局命名空间
- Catalina C++:使用<cmath>标头产生错误:全局命名空间中没有名为 'signbit' 的成员
- 将以"_[a-z0-9]"开头和"using"的标识符导入全局命名空间是否定义良
- 为什么不允许在全局命名空间处阻止范围?
- 运算符<<依赖于参数的查找不在全局命名空间中查找
- 我可以停止 GCC 将标准库名称包含在全局命名空间中吗?
- "uintmax_t":不是"全局命名空间"的成员
- C++项目中的全局命名空间
- 使用 "::member" 引用全局命名空间有什么用吗?
- 在全局命名空间中重载不依赖于用户定义类型的标准定义类型的运算符是否格式正确?
- 使用全局命名空间限定符和指向数据成员的指针
- InitializeCriticalSectionEx 不是 atlwinverapi.h 中全局命名空间的成员
- C++在命名空间中声明与全局命名空间中的函数同名的函数
- 在vs2010中使用iostream时,出现错误C2039:'exit':不是"全局命名空间"的成员
- 是否可以恢复到"default"全局命名空间?
- C++错误代码全局命名空间
- equal()是否默认包含在全局命名空间中
- 前向声明和全局命名空间声明
- C++在全局命名空间中找不到非标准 C 函数