Why can't I separately write the namespace hierarchy in the header file?

I am writing some header file. I want to declare the namespace hierarchy separately (for clarity), and then declare the functions and classes. For me it looks like a table of contents in a document. It would be very convenient for me: to see the entire hierarchy of namespaces in one place. I am writing this:

// Namespaces hierarchy: namespace Bushman{ namespace CAD_Calligraphy{} //... } // Declarations of classes and functions class Bushman::CAD_Calligraphy::Shp_ostream{ public: explicit Shp_ostream(std::ostream& ost); }; 

But MS Visual Studio is shouting about such a way to create a header file. I have to write like this:

 namespace Bushman{ namespace CAD_Calligraphy{ class Shp_istream{ public: explicit Shp_istream(std::istream& ist); }; } } 

Why does the first option not work? Is this a limitation of C ++ or IDE?

PS My additional question is here .

Thanks.

+4
source share
4 answers

The limitation in Β§ 9/1 is: β€œIf the name of the header class contains a sub-name specifier, the class specifier must refer to a class that was previously declared directly in the class or namespace to which the sub-name specifier [...] refers." in other words, the first appearance of a class name cannot be in something like Bushman::CAD_Calligraphy::Shp_ostream .

What you can do is add forward declarations in your initial hierarchy declaration:

 // Namespaces hierarchy: namespace Bushman{ namespace CAD_Calligraphy{ class Shp_ostream; //... } //... } // Declarations of classes and functions class Bushman::CAD_Calligraphy::Shp_ostream{ public: explicit Shp_ostream(std::ostream& ost); }; 

Depending on how your headings are organized, this may be even better from a human point of view: your headline starts with a kind of index of what is defined in it.

+10
source

To quote the standard: Section 7.3.1.2, paragraph 2:

Members of a named namespace can also be defined outside this namespace by explicitly qualifying (3.4.3.2) the name is determined if the declared entity is already defined in the namespace and the definition appears after the dot in the namespace that encompasses the namespace of the declaration.

 namespace Q { namespace V void f(); } void V::f() { /βˆ— ... βˆ—/ } // ok. void V::g() { /βˆ— ... βˆ—/ } // Error: g() is not yet a member of V namespace V void g(); } } namespace R { void Q::V::g() { /βˆ— ... βˆ—/ } // // error: R doesn't enclose Q } 

So, you can do what you have in your original post if you declare the class name there:

 namespace Bushman{ namespace CAD_Calligraphy { class Shp_ostream; ... } } 
+2
source

How does C ++ work.

This is consistent with other nested declarations: you cannot add members to a class outside the class:

 class A { }; void A::f() { } // Error! 

And you cannot add enumerations to an enumeration from outside:

 enum E { E1 = 1, E2 = 2 }; E::E3 = 3; // Error! 

You need to β€œopen” the scope and declare an object inside the scope. Once it is declared, you can define it outside this scope using the name-sub-name:

 class A { void f(); // declare }; void A::f() { } // define 
+1
source

First of all, C ++ is not intended for such work. Therefore, it is not surprising that this is happening.

But, since you are using Visual Studio, you can use partial classes . Unfortunately, it seems that this characteristic is only related to C ++ / CX, so perhaps yo will not be able to use it.

You still need to declare a partial class in the namespace hierarchy, but I think it might be empty.

Honestly, I have not used this feature, and I do not know how far it can be bent to achieve what you want. But you can still try.

Remember, this is a Visual Studio extension, so your code will not be cross-platform.

Hope this helps. Somehow.

-1
source

Source: https://habr.com/ru/post/1491079/


All Articles