从其他头文件实现结构
Implementing structure from other header file
我必须在类头中有一个结构"configset",类似于以下内容:
class A {
public:
static configset *getnext();
}
当然,编译器抱怨configset不是一个类型,但它是在另一个头文件中实现的,还有一些函数。正因为如此,我不能只包含那个头文件,编译器会抛出这些函数被多次定义的错误。但是我怎样才能在类头中获得配置集呢?仅仅复制也不起作用,因为编译器会抛出一个错误,即结构被定义了两次。
转发声明configset
或将头文件中定义的函数声明为inline
。
使用转发声明,如下所示:
// Forward declare configset. Tells compiler that the class/struct is defined in
// another translation unit
struct configset;
class A {
public:
static configset *getnext();
};
请注意,只有当您只使用指向configset的指针时,这才有效。
将configset
方法的实现移动到一个单独的.cpp(而不是头)文件中。
相关文章:
- 如何使用set实现无序数据结构?
- 如何从实现接口的模板化类实例访问结构
- C++:实现向量和矩阵类的最佳结构
- 如何实现返回受保护结构的私有函数
- 在 C++14 中手动实现结构化绑定
- C++ 在外部使用嵌套结构实现类函数
- 如何知道用于实现标准代码段的确切数据结构和算法,例如在C++STL中?
- 如何在C++中实现节点数任意的通用树数据结构?
- 如何在层次结构中实现运算符使用?
- 使用智能指针指向 C 库中的结构,该结构通过 typedef 隐藏实现(即不完整的类型)
- C++ 中的 TRIE 数据结构实现
- 如何实现 Boost.Hana 结构的相等比较运算符?
- 嵌套的模板结构构造函数实现
- 与现有VAR无法实现结构化结合
- 难以理解链表实现(结构部分)?
- 当您知道结构将来会更改时,实现结构读写的最佳方法是什么
- 为什么我在这个实现结构的C++程序中出现错误
- .h 文件中的客户端结构与实现结构
- 从其他头文件实现结构
- 实现结构指针的自定义比较器