I am confused what to do when having nested namespaces and declarations of objects.
I am porting some code that links against a static library that has a few namespaces.
Example of what I am talking about:
namespace ABC {
namespace XYZ {
//STUFF
}
}
In code what do I do to declare an object that is in namespace XYZ?
if I try:
XYZ::ClassA myobject;
or:
ABC::XYZ::ClassA myobject;
or:
ABC::ClassA myobject;
I get
does not name a type
errors, even though ClassA definitely exists.
What is proper here?
It depends on the namespace you already are:
If you’re in no namespace or another, unrelated namespace, then you have to specify to whole path
ABC::XYZ::ClassA.If you’re in
ABCyou can skip theABCand just writeXYZ::ClassA.Also, worth mentioning that if you want to refer to a function which is not in a namespace (or the “root” namespace), you can prefix it by
:::Example: