C++ explanation of two compilation errors related to namespace
- 2020-06-15 09:56:53
- OfStack
Once, in a large project code, I call:
#include <iostream>
using namespace std;
namespace A
{
void fun()
{
printf("aaa\n");
}
}
namespace B
{
void fun()
{
printf("bbb\n");
}
}
int main()
{
fun();
return 0;
}
Compilation error:
error: ‘fun' was not declared in this scope
, checked 1, the original is a space in mischief. By the way, why not indent a function in a namespace? I thought about it for a second and understood why the people who were writing the code were doing it.
Here's another mistake I encountered:
#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;
}
Results:
call of overloaded ‘fun()' is ambiguous
The mistake is obvious. In practice, it is a common mistake.
conclusion