Reputation: 3
I've got C++ code separated into 2 libraries (DLLs) and each library has a function w/ the same name, although each is in a different namespace. So in library 1, I have something like the following.
namespace LibOne
{
void Foo(ClassOne& src);
}
In library 2, I have classes that were accessing LibOne::Foo w/o a problem as follows.
using namespace LibOne;
namespace LibTwo
{
class MyClass
{
...
void MyFunc()
{
Foo(src);
}
}
}
Then in library 2, I added additional functions w/ the same name.
namespace LibTwo
{
void Foo(ClassTwo& src);
void Foo(ClassThree& src);
}
Now the usage of LibOne::Foo, issues a compiler error
error C2665: 'LibOne::Foo': none of the 2 overloads could convert all the argument types
I can resolve the error by changing my code as follows to specify the full name of the function, but I'm just trying to understand why the addition of the function in LibTwo appears to hide the versions of the function from LibOne? Is there any way to prevent this from happening?
LibOne::Foo(src)
Upvotes: 0
Views: 77
Reputation: 66961
No, only the innermost scope that contains a name will be examined. There is Foo
in namespace LibTwo
, so the compiler doesn't even notice there's also a Foo
imported into the global namespace.
In general, avoid using namespace
, and prefer to explicitly qualify names. LibOne::Foo
Upvotes: 1