0

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.

2) The struct I reference in 1) is in a general header file which does not reference other structs. 3) When I include general.h, it won't compile for other structs in 1) header file.

Here is 1) my .h file, it include general.h file in order to reference IntervalFilter:

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

    other structs
...
}

But general.h file include 1) and reference other structs inside the file.

Any advice as to the syntax?

Thanks!

Ann W.
  • 27
  • 1
  • 7
  • 3
    Can you rephrase your question. This is completely unparsable: "I've a .h header file that How to cross reference a struct in a general header file that back referencing other structs inside my .h header file." -- this does not compute. – Sam Varshavchik Jul 02 '16 at 22:47
  • Please see the above. – Ann W. Jul 02 '16 at 22:50
  • This is better, but still very confusing: "A .h header file that cross reference a struct in a general header file". A header file does not "cross reference" anything. A header file contains declarations of classes, templates, and other data structures. "that back referencing other structs inside my .h header file." What is "back referencing"? Is "my .h header file" the same header file mentioned at the beginning or another header file? Try to forget everything that you know, right now, then read your question and try to figure out what it means. – Sam Varshavchik Jul 02 '16 at 22:54
  • if you are creating a circular reference, then consider using `pragma once`. – meJustAndrew Jul 02 '16 at 22:58
  • http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies-in-c?rq=1 – kfsone Jul 03 '16 at 00:20

1 Answers1

0

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.

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
  2. Use forward declarations instead of including the header file (won't work if you need a complete type, e.g. not pointer/reference)
kikobyte
  • 334
  • 1
  • 10