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

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




本文转载:CSDN博客