为什么我不能在头文件中单独写入名称空间的层次结构? [英] Why I can't separately write namespace's hierarchy in the header file?
问题描述
我写了一些头文件.我想分别声明名称空间层次结构(为清楚起见),然后声明函数和类.对我来说,它看起来像是文档中的目录.对我而言,这将非常方便:在一处看到名称空间的完整层次结构.我这样写:
I write some header file. I want separately declare the namespaces hierarchy (for clarity), and and then declare functions and classes. For me it looks as a table of contents in the document. It would be very convenient for me: to see the full hierarchy of namespaces in one place. I write 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);
};
但是,MS Visual Studio对创建头文件的这种方式大喊大叫.我应该这样写:
But MS Visual Studio shouts on such way of creation of the header file. I should write so:
namespace Bushman{
namespace CAD_Calligraphy{
class Shp_istream{
public:
explicit Shp_istream(std::istream& ist);
};
}
}
为什么第一个变体不起作用?这是C ++或IDE的限制吗?
Why the first variant doesn't work? Is this restriction of the C++, or IDE?
P.S.我的其他问题是此处.
P.S. My additional question is here.
谢谢.
推荐答案
该限制在§9/1中:如果类头名称包含
嵌套名称说明符,类说明符应引用
先前直接在该类中声明的类,或者
嵌套名称说明符引用的名称空间[...].
换句话说,类名的首次出现不能在
类似于Bushman::CAD_Calligraphy::Shp_ostream
.
The restriction is in §9/1: "If a class-head-name contains
a nested-name-specifier, the class-specifier shall refer to
a class that was previously declared directly in the class or
namespace to which the nested-name-specifier refers[...]". In
other words, the first appearance of the class name cannot be in
something like Bushman::CAD_Calligraphy::Shp_ostream
.
您可以做的是在您的初始姓名中添加前向声明 层次结构的声明:
What you can do is add forward declarations in your initial declaration of the hierarchy:
// 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 headers are organized, this might even be better from the human point of view: your header starts with a sort of index of what is defined in it.
这篇关于为什么我不能在头文件中单独写入名称空间的层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!