There are two forms of WinForms that must exchange data with each other. As you know, for this you need to include a header file of another form. But it turns out that the form, including another form, includes itself - the process becomes infinite. As a result, an error is displayed - "Too many include files". How can this be fixed?

  • one
    Do not mix the presentation and data, selecting for the latter a separate file with the class for storage. - Alex Krass

2 answers 2

Usually the header file has the structure

// ===== Начало файла ======= // Если не определен символ MY_FILE_H #ifndef MY_FILE_H // определить его #define MY_FILE_H // здесь остальные определения и инклуды #endif // ====== конец файла ======= 

then, no matter how many times you turn it on, it will turn on once, and then MY_FILE_H will determine the character and the remaining times the #ifndef MY_FILE_H will not work

  • #pragma once does not the same? - LLENN 8:36 pm
  • However, there is no real benefit from this. The infinite inclusion will stop, but the initial task will not be solved by this. Cyclic inclusion should not be in principle. To create a cyclic inclusion, and then break it with the help of include guards - pointless work. - AnT 8:39 pm
  • I’ll add to the @AnT comment that the TC "y should just define an incomplete class type managed, like this: ref class Form2; in the header file use the incomplete type, and already in the implementation file .cpp connect the header with the full type - LLENN

There are 2 options to solve your problem.

  1. Determine the incomplete type in the headers that interest you.
  2. Include all headers in the 'stdafx.h' file.

Definition of incomplete type:

Form1.h

 ref class Form2; public ref class Form1 { Form1(Form2^ form); ... }; 

Form1.cpp

 #include "Form1.h" #include "Form2.h" Form1::Form1(Form2^ form) { ... } 

Form2.h

 ref class Form1; public ref class Form2 { void SomeMethod(Form1^ form); ... }; 

Form2.cpp

 #include "Form2.h" #include "Form1.h" void Form2::SomeMethod(Form1^ form) { ... } 

Everything, the problem is solved, there is no looping or duplication of types.