Home > Back-end >  The humble small white online for help
The humble small white online for help

Time:09-27

When two classes to be with each other to define types should be how to write ah
I have A class A and class B, A member of the type of the variable A b_type in B, B in the member variables of B type is A a_type, how do you write to no error
 
Class A {
Public:
Using a_type=int;
B: : b_type a;
}
Class B {
Public:
Using b_type=int;
A: : a_type b;
}

CodePudding user response:

 class B;//the predefined 
Class A {
Public:
Using a_type=int;
B: : b_type a;
}
Class B {
Public:
Using b_type=int;
A: : a_type b;
}

CodePudding user response:

reference 1st floor suifengsanjin response:
 class B;//the predefined 
Class A {
Public:
Using a_type=int;
B: : b_type a;
}
Class B {
Public:
Using b_type=int;
A: : a_type b;
}

This seemingly can't solve B: : b_type??????

CodePudding user response:

1. The lack of forward declaration: the class B;

2. The lead statement only as a pointer or reference, cannot define the object of the class, also can't call object method

The solution: separate the declaration and implementation, can be defined in the realization of the corresponding B: : b_type type of a variable
  • Related