Can two headers contain indentically named classes in C++?
And both be included by a project, if they are in different namespaces? I'm 开发者_如何学运维having a problem just including them because of duplicity...
Yes and no.
The name of the class Foo
in namespace1
is "namespace1::Foo"
The name of the class Foo
in namespace2
is "namespace2::Foo"
These names are not identical - you cannot have two identically named classes in different namespaces.
Obviously, both have a local name of "Foo" and if you do something like
using namespace namespace1;
using namespace namespace2;
Foo x;
then there will be an ambiguity, as you've told the compiler to ignore the different bit of the names.
But as long as you don't bring both into scope, you can use the full name to use either of them as you wish.
Yes, if they are in different namespaces, they don't really have the same name.
Show us your headers & your CPP file, but please abbreviate them!
Yes if they are in different namespaces of you don't use both of them in a third location.. are you sure the problem is related to the names and not something like cyclic references?
As long as the two classes are in different namespaces, you can include them in the same file without problems. C++ only complains if it finds two definitions for classes with the same fully-qualified name, and fully-qualified names include the namespace.
Your problem is likely elsewhere... as mentioned above, posting code will make it easier for people to diagnose the problem.
精彩评论