Today in the C + + write neural network algorithm, found a very puzzling problem, roughly described as follows:
I have two classes ClassA and CLASSB, each with member variables A, b ;
ClassA has a function like this: Functiona (const ClassB &CLASSB)
There is a similar function in CLASSB: functionb (const CLASSA&CLASSA)
The first code is similar to this, but the error, the reason is very simple, classb in Functiona when using CLASSB, CLASSB only declaration, not defined .
Class ClassB; < advance declaration Classbclass classa{public:void Functiona (const ClassB &CLASSB); int A;}; void Classa::functiona (const ClassB &CLASSB) {int k = classb.b; < here is an error, using an undefined classb}class classb{public:void functionb (const ClassA *classa) {int k = classa->a;}; int b;}; void functionb (const ClassA &classa) {int k = CLASSA.A;} int main () {return 0;}
Since the problem is: CLASSB only declaration, not defined. To solve the above problem, here's one thing to do:
is to put the implementation of Functiona in the definition of CLASSB . .
Class ClassB; < advance declaration Classbclass classa{public:void Functiona (const ClassB &CLASSB); int A;}; Class Classb{public:void functionb (const ClassA *classa) {int k = classa->a;}; int b;};/ < Functiona after the definition of ClassB, there is no problem. void Classa::functiona (const ClassB &CLASSB) {int k = classb.b; } void functionb (const ClassA &classa) {int k = CLASSA.A;} int main () {return 0;}
Duanxx C + + Learning: Using undefined class causes and solutions