C++与namespace有关的两个编译错误的讲解
2019-04-11 09:17
1131 查看
某次,在大型的工程代码中,我这样调用:
#include <iostream> using namespace std; namespace A { void fun() { printf("aaa\n"); } } namespace B { void fun() { printf("bbb\n"); } } int main() { fun(); return 0; }
编译出错:error: ‘fun' was not declared in this scope
,查了一下,原来是名空间在捣鬼。另外,名空间中的函数为什么不缩进呢?我想了一下,理解了当时写代码的人为什么要这样做。
再看我遇到的另外一次错误:
#include <iostream> using namespace std; namespace A { void fun() { printf("aaa\n"); } } namespace B { void fun() { printf("bbb\n"); } } using namespace A; using namespace B; int main() { fun(); return 0; }
结果:call of overloaded ‘fun()' is ambiguous
,错误很显而易见了。实际中也确实经常会犯这种错误。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接
您可能感兴趣的文章:
相关文章推荐
- 与namespace有关的两个编译错误
- C++ 编译过程中报 error C2039: “div_t”: 不是“`global namespace'”的成员 的错误
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 有关C++模板(template)的编译错误“error LNK2019: 无法解析的外部符号”的分析
- 有关C++模板(template)的编译错误“error LNK2019: 无法解析的外部符号”的分析
- 有关C++模板(template)的编译错误“error LNK2019: 无法解析的外部符号”的分析
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 两个C++编译错误及解决办法--does not name a type和field `XX' has incomplete type
- 两个小问题解决记录:XCode中多Target时CFBundleDisplayName的多语言本地化;用C++写socket程序时编译错误说read和write找不到
- iOS SDK开发时,有关OC和C++混编造成其它类错误的问题(Compile Sources As Objective-C++)
- c++中两个类的头文件互相包含编译出错的解决办法
- C/C++常见gcc编译链接错误解决方法
- C++头文件注意要点 & 编译错误常见错因
- C++编译错误解决
- DSP中由C转成C++出现编译 badly formed pragma错误的解决.
- google-breakpad在 C++11下编译错误 ISO C++ forbids declaration of ‘typeof’ with no type
- C++编译错误:multiple types in one declaration