通过 header 文件 C++ 使用多个结构
Using multiple structs through header files C++
抱歉,如果您以前见过这个问题,但它尚未得到回答,基本上在我的代码中我有两个结构,在单独的 header 中定义并在整个项目中全局使用。我只是希望在其他 cpp 文件中使用这两个结构(同样,它们在两个单独的 header 中定义),而不仅仅是 header 文件所属的那些。
这是我测试过的一些示例代码:
class1.h
#include "class2.h"
#include <vector>
#include <string>
struct trans1{
string name;
};
class class1 {
private:
vector <trans2> t2;
public:
class1();
};
class2.h
#include "class1.h"
#include <vector>
#include <string>
struct trans2{
string type;
};
class class2{
private:
vector <trans1> t1;
public:
class2();
};
错误日志:
In file included from class1.h:3:0,
from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
vector <trans1> t1;
^
class2.h:21:19: error: template argument 1 is invalid
vector <trans1> t1;
^
class2.h:21:19: error: template argument 2 is invalid
我知道这在现实世界的应用程序中是荒谬的代码,但这是我可以演示的最简单的方法。
值得注意的是,如果我简单地注释掉 'private:' 下的向量 t1 或 t2 的声明,代码将不会失败。这只是我正在使用第二个结构的事实。
有什么帮助吗?谢谢
只需 forward-declare 将使用的 classes。把所有的实现代码放到一个cpp文件里,不要内联在header.
将向量设为私有。这样,任何包含 header 的文件都无法针对不完整的 class.
强制生成代码
您可以像这样尝试在 class2.h 中转发声明 trans1 并在 class1.h 中转发声明 trans2:
class2.h :
// includes
struct trans1;
// rest of your code
在 class1.h
中有同样的事情(但有 trans2)
不要忘记在代码中添加 Include guards!
- 编辑:是的,您需要更改向量以存储指针,否则它不会 link
如果您在单个 .cpp 文件中执行此操作,解决方案将很简单:
struct trans1 { ... };
struct trans2 { ... };
class class1 { ... };
class class2 { .... };
现在您只需重新排列代码即可在每个翻译单元中获得此结果。 (文件中 classes/structs 的顺序很重要)
您需要将 "trans" 结构放入它们自己的头文件中,并将它们包含在您的 class 头文件中。
您可以转发声明它们,但这需要更改您的向量以使用指针。 (在那种情况下,我会推荐 std::vector<std::unique_ptr<trans>>
)。如果您的结构又大又复杂,这可能是合适的。
前向声明方法的主要优点是减少编译时间。但是,如果结构真的像您的示例中那样简单,那么我不会为在这里使用指针的额外开销而烦恼。
抱歉,如果您以前见过这个问题,但它尚未得到回答,基本上在我的代码中我有两个结构,在单独的 header 中定义并在整个项目中全局使用。我只是希望在其他 cpp 文件中使用这两个结构(同样,它们在两个单独的 header 中定义),而不仅仅是 header 文件所属的那些。 这是我测试过的一些示例代码:
class1.h
#include "class2.h"
#include <vector>
#include <string>
struct trans1{
string name;
};
class class1 {
private:
vector <trans2> t2;
public:
class1();
};
class2.h
#include "class1.h"
#include <vector>
#include <string>
struct trans2{
string type;
};
class class2{
private:
vector <trans1> t1;
public:
class2();
};
错误日志:
In file included from class1.h:3:0,
from class1.cpp:1:
class2.h:21:13: error: 'trans1' was not declared in this scope
vector <trans1> t1;
^
class2.h:21:19: error: template argument 1 is invalid
vector <trans1> t1;
^
class2.h:21:19: error: template argument 2 is invalid
我知道这在现实世界的应用程序中是荒谬的代码,但这是我可以演示的最简单的方法。
值得注意的是,如果我简单地注释掉 'private:' 下的向量 t1 或 t2 的声明,代码将不会失败。这只是我正在使用第二个结构的事实。
有什么帮助吗?谢谢
只需 forward-declare 将使用的 classes。把所有的实现代码放到一个cpp文件里,不要内联在header.
将向量设为私有。这样,任何包含 header 的文件都无法针对不完整的 class.
强制生成代码您可以像这样尝试在 class2.h 中转发声明 trans1 并在 class1.h 中转发声明 trans2:
class2.h :
// includes
struct trans1;
// rest of your code
在 class1.h
中有同样的事情(但有 trans2)不要忘记在代码中添加 Include guards!
- 编辑:是的,您需要更改向量以存储指针,否则它不会 link
如果您在单个 .cpp 文件中执行此操作,解决方案将很简单:
struct trans1 { ... };
struct trans2 { ... };
class class1 { ... };
class class2 { .... };
现在您只需重新排列代码即可在每个翻译单元中获得此结果。 (文件中 classes/structs 的顺序很重要)
您需要将 "trans" 结构放入它们自己的头文件中,并将它们包含在您的 class 头文件中。
您可以转发声明它们,但这需要更改您的向量以使用指针。 (在那种情况下,我会推荐 std::vector<std::unique_ptr<trans>>
)。如果您的结构又大又复杂,这可能是合适的。
前向声明方法的主要优点是减少编译时间。但是,如果结构真的像您的示例中那样简单,那么我不会为在这里使用指针的额外开销而烦恼。