C++与namespace有关的两个编译错误的讲解
作者:stpeace 时间:2021-12-09 11:37:35
某次,在大型的工程代码中,我这样调用:
#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
,错误很显而易见了。实际中也确实经常会犯这种错误。
来源:https://blog.csdn.net/stpeace/article/details/73380509
标签:c++,namespace,编译错误
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java后台实现微信支付和微信退款
2023-09-06 13:44:00
![](https://img.aspxhome.com/file/2023/2/92692_0s.png)
C#实现判断当前操作用户管理角色的方法
2023-06-25 09:48:36
SpringBoot如何访问html和js等静态资源配置
2023-08-17 08:07:43
![](https://img.aspxhome.com/file/2023/7/63787_0s.png)
android Matrix实现图片随意放大缩小或拖动
2022-02-12 12:06:48
![](https://img.aspxhome.com/file/2023/6/88676_0s.jpg)
Android 中解决Viewpage调用notifyDataSetChanged()时界面无刷新的问题
2022-11-11 22:21:50
hadoop实现grep示例分享
2023-12-24 05:31:04
Spring Data Jpa框架最佳实践示例
2021-11-25 00:43:01
C# Dynamic关键字之:解析dynamic就是Object
2022-09-16 15:08:16
![](https://img.aspxhome.com/file/2023/0/116620_0s.png)
SprintBoot深入浅出讲解场景启动器Starter
2023-11-24 20:58:58
![](https://img.aspxhome.com/file/2023/9/59419_0s.png)
C++之异常处理详解
2023-04-10 10:22:00
![](https://img.aspxhome.com/file/2023/3/114923_0s.jpg)
Spring Data环境搭建实现过程解析
2022-02-26 20:13:38
Java ThreadLocal类使用详解
2022-08-04 05:29:34
![](https://img.aspxhome.com/file/2023/4/71014_0s.png)
如何基于Java实现对象List排序
2022-01-28 03:59:16
java数据结构与算法之快速排序详解
2023-02-23 10:23:43
Ubuntu16.04下配置VScode的C/C++开发环境
2023-10-22 19:21:17
![](https://img.aspxhome.com/file/2023/6/123856_0s.jpg)
适用于WebForm Mvc的Pager分页组件C#实现
2022-05-11 22:11:34
解决mybatis一对多查询resultMap只返回了一条记录问题
2022-07-08 08:36:31
C#中调用命令行cmd开启wifi热点的实例代码
2023-11-28 03:15:04
![](https://img.aspxhome.com/file/2023/0/96350_0s.jpg)
详解C#开发Android应用程序的流程
2021-08-11 21:32:59
![](https://img.aspxhome.com/file/2023/9/84409_0s.png)
Android编程实现获得内存剩余大小与总大小的方法
2023-06-07 05:49:17