简体   繁体   English

如何在普通文件中交叉引用反向引用其他结构的结构

[英]How to cross reference a struct in a general file that back referencing other struct

I've two files: 1) A .h header file that cross reference a struct in a general header file that back referencing other structs inside my .h header file. 我有两个文件:1)一个.h头文件,该文件交叉引用通用头文件中的结构,该文件在我的.h头文件中向后引用其他结构。

2) The struct I reference in 1) is in a general header file which does not reference other structs. 2)我在1)中引用的结构在一个常规头文件中,该文件不引用其他结构。 3) When I include general.h, it won't compile for other structs in 1) header file. 3)当我包含general.h时,它将不会为1)头文件中的其他结构编译。

Here is 1) my .h file, it include general.h file in order to reference IntervalFilter: 这是1)我的.h文件,它包含general.h文件以引用IntervalFilter:

 ...
 struct AgentConfig {
    AgentConfig();
    IntervalFilter<int> myFilter;

    other structs
...
}

But general.h file include 1) and reference other structs inside the file. 但是general.h文件包括1)并引用文件中的其他结构。

Any advice as to the syntax? 关于语法有什么建议吗?

Thanks! 谢谢!

From what I understood you have two headers which include each other. 据我了解,您有两个包含彼此的标头。 You should never allow this situation, because preprocessing will yield one header above the other (given you have header guards or #pragma once), and one of those headers will always miss the stuff it needs. 您绝对不应允许这种情况,因为预处理将在另一个头之上产生一个头(假设您具有头保护或#pragma一次),并且这些头之一始终会丢失所需的内容。

There are a few ways to solve this situation: 有几种方法可以解决这种情况:

  1. Extract the independent stuff (the "other structs" you were referring to) into a third header file and resolve the 1 <-> 2 loop into a "triangle" 1 -> 3, 2 -> 3 and 1 -> 2 将独立的内容(您所指的“其他结构”)提取到第三个头文件中,并将1 <-> 2循环解析为“三角形” 1-> 3、2-> 3和1-> 2
  2. Use forward declarations instead of including the header file (won't work if you need a complete type, eg not pointer/reference) 使用前向声明而不是包含头文件(如果需要完整的类型(例如,不包括指针/引用),则不起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM