首页 > 编程开发 > C类语言    日期:2021-06-03 / 浏览

某次,在大型的工程代码中,我这样调用:

#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 ,错误很显而易见了。实际中也确实经常会犯这种错误。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章