If you create two types of structures A and B :

 typedef struct A { A(B b) {}; } A; typedef struct B { B(A a) {}; } B; 

Then an error will appear for A

Unknown type name 'B'

since it is announced later. If you declare B before A , then an error will appear on constructor B Is there something like function prototypes, but for custom types and classes?

  • Can you explain why in C ++ you need to define a typedef structure with the name of the structure, when and so you can refer to the name of the structure without typedef? - Vlad from Moscow

2 answers 2

You can use references and pointers to types that will be defined later using forward declaration:

 struct B; struct A { A(B& b) {}; }; struct B { B(A& a) {}; }; 

But you can't just use a type in a call — for example, the size of this type to the compiler is unknown before the full declaration.

PS typedef struct A { ... } A; in C ++ is not needed.

  • And now try to create an object of one of these structures. - Vlad from Moscow
  • @VladfromMoscow struct A { A(){} A(B& b) {}; }; struct B { B(A& a) {}; }; int main() { A a; B b(a); } struct A { A(){} A(B& b) {}; }; struct B { B(A& a) {}; }; int main() { A a; B b(a); } struct A { A(){} A(B& b) {}; }; struct B { B(A& a) {}; }; int main() { A a; B b(a); } Or do you think that the answers should be given the full text of the program, and not explaining fragments? - Harry
  • @ Harry I ask you about the original question, and not about your own fictional class. - Vlad from Moscow
  • @VladfromMoscow Sophistika. If not demagoguery. I have no time to engage in an altercation with empty cavils. Do you have time - go ahead, and I am "not an intellectual, I have a profession" (c) :) - Harry
  • In fact, from a purely declarative point of view, there is no formal need to go to the links: C ++ perfectly permits the use of incomplete types in function declarations (in parameter declarations and return type). These types should be complete only by the time these functions are defined . Thus, in this case, you can save the transmission by value, you just need to make the definition of A::A beyond the definition of class A and place it after the definition of class B - AnT

In C ++, it does not make much sense to introduce name aliases for structures in this way, since in C ++ you can refer to a structure by its name without the keyword struct . This makes sense only in C, but in C there are no constructors for structures. Therefore, it is not clear what you are trying to achieve.

But even if you write it so that the code is compiled, such as

 typedef struct AA; typedef struct BB; struct A { A( B ); }; struct B { B( A ); }; A::A( B ) { /*...*/ } B::B( A ) { /(...(/ } 

However, you cannot create any objects of these structures, since you have a cyclic dependency of constructor calls.

Therefore, in order for the code to make sense, you need to add an additional constructor to at least one of the structures, which does not depend on the object of another class in order to avoid the cyclic dependence of constructor calls.